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:

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. 

Asking for an object's class using isKindOfClass: is a definite code smell.

Cheers,
-- Uli Kusterer
"The Witnesses of TeachText are everywhere..."
http://www.masters-of-the-void.com




_______________________________________________

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