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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With