Re: blocks and id
Re: blocks and id
- Subject: Re: blocks and id
- From: Uli Kusterer <email@hidden>
- Date: Sat, 15 Dec 2012 01:44:24 +0100
On 15.12.2012, at 01:38, Uli Kusterer <email@hidden> wrote:
> On 12.12.2012, at 10:03, Andreas Grosam <email@hidden> wrote:
>> How can I check at runtime whether an object (id) is actually a block, and not another kind of object?
>
> Not a good idea. What are you really trying to do? Here's a few common cases and suggestions on how to do it better, and why:
>
> 1) Serializing objects. Generally, the object (or a category on it if it's an object you didn't create) should implement a method that knows how to serialize/unserialize it, like -initWithCoder: and -encodeWithCoder:. This allows any class to be added, and allows for overriding a the method in a subclass. If you use -isKindOfClass:
Drat, accidentally hit 'send'. That should be If you use -isKindOfClass:, all subclasses will also return YES, and that one method will have to know about all the different types (from different layers of your app that might be serialized), and you'll have one file with thousands of dependencies, that get dragged into any other app that wants to be able to use the same serialization mechanism.
> 2) Implementing special behaviour on some objects, while falling back on some default behaviour for all others. Call respondsToSelector: in this case. It has the advantage that it doesn't break duck typing. Even if you get an NSProxy for the actual object, it will respond to the selector and still work as expected.
Same for any other kind of method forwarding or dynamic method implementation like Key-Value-Observing.
> Asking for an object's class using isKindOfClass: is a definite code smell.
Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden