Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is id a "root type", or just a type?

Tags:

objective-c

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?

like image 458
CodaFi Avatar asked Jun 10 '13 15:06

CodaFi


1 Answers

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.

like image 180
Carl Norum Avatar answered Oct 17 '22 07:10

Carl Norum