On 15.12.2012, at 01:38, Uli Kusterer <witness.of.teacht...@gmx.net> wrote:

> On 12.12.2012, at 10:03, Andreas Grosam <agro...@onlinehome.de> 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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to