Reputation: 43330
Many Objective-C developers, both novice and expert, seem to regard id
as though it is some magical catch-all type that you can cast an object to and send any old message to without consequence (from the compiler, at least). But, when one actually looks at the definition of id
, it's nothing more than a pointer to an objc_object
. In this way, it is literally an "Objective-C object," but nothing can "descend" from id
. Even NSObject's header doesn't look like:
@interface NSObject : id
It just reads:
@interface NSObject
Now, I'm firmly of the belief that id
's supposed type neutrality is a side-effect of its implementation, but because there are those that still think otherwise, I've got to ask:
What exactly is the primary function of the id
type?
Upvotes: 10
Views: 188
Reputation: 224864
id
is just a generic object pointer, as you've found. Since it's not a class, nothing can inherit from it. NSObject
, strictly speaking, isn't part of the Objective-C language itself, just part of a particular implementation.
id
is convenient for a language like Objective-C that has such weak typing; it allows you to keep generic object pointers around and send them arbitrary messages.
Upvotes: 10