Re: NSObject vs id

Christiaan Hofman <[email protected]>
Newsgroups gmane.comp.macosx.devel
Message-ID <[email protected]>
On Sep 3, 2009, at 18:31, Kyle Sluder wrote:

> On Thu, Sep 3, 2009 at 9:05 AM, Florent
> Lacheroy<[email protected]> wrote:
>> I wonder wether it is better to use NSObject or id.
>
> The compiler doesn't perform static type checking on expressions of
> type id.  So as long as someone, somewhere has declared a
> -doFoo:withBar:baz: method, I can send that message to an id.
>
> Once you specify NSObject, though, the compiler assumes that your
> object only responds to messages in that interface/protocol.  Makes
> "NSObject *foo" kind of useless unless you only need to send NSObject
> messages to it.
>
> --Kyle Sluder

Also don't forget that not all classes derive from NSObject. For  
instance there is another root class NSProxy, and then there are  
generic toll free bridged objects which have class NSCFType.

The basic rule is that you can assume that all objects (type id)  
should at least respond to the basic memory management methods and - 
description (basically the Cocoa counterparts of CFBase.h). For other  
methods you should first know they responds, usually using  
respondsToSelector: (or you know it from a context, e.g. when the  
object is a delegate or dataSource with required methods).

Christiaan
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.