On Thu, May 27, 2010 at 8:25 PM, Philip Vallone
<[email protected]> wrote:
>
> This is a relative question, which depends on how the data is coming and
> going. My question comes from the following situation. Suppose I have a
> GKSession that is passing information via Bluetooth. The sender can send any
> type of information (NSString, UIImage etc...). The receiver needs to know
> how to handle this data. If there is a better way... Then how?
>
> I appreciate the feed back and help,
I would let the sent objects handle the work themselves. A switch or
series of ifs based on class is an OOP anti-pattern. Polymorphism is
often a better alternative, and Objective-C's ability to add a
category to any class makes it easy to implement. So, I would extend
NSString, UIImage, etc. - whatever types can be sent - by adding a new
method "mySuperDuperMethod" (for example).
Then, what you're left with in the receiver class is simply:
if ([obj respondsToSelector(@selector(mySuperDuperMethod))]) {
[obj performSelector:@selector(mySuperDuperMethod)];
}
If the ability of a sent object to implement mySuperDuperMethod is
critical, you could add an else block to log and/or assert any such
failures.
sherm--
--
Cocoa programming in Perl:
http://www.camelbones.org
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]