What is the meaning of id?

GPP picture GPP · Nov 2, 2011 · Viewed 63.6k times · Source

I am (trying to) learn Objective-C and I keep coming across a phrase like:

-(id) init;

And I understand id is an Objective C language keyword, but what does it mean to say "the compiler specifically treats id in terms of the pointer type conversion rules"?

Does id automatically designate the object to its right as a pointer?

Answer

joerick picture joerick · Nov 2, 2011

id is a pointer to any type, but unlike void * it always points to an Objective-C object. For example, you can add anything of type id to an NSArray, but those objects must respond to retain and release.

The compiler is totally happy for you to implicitly cast any object to id, and for you to cast id to any object. This is unlike any other implicit casting in Objective-C, and is the basis for most container types in Cocoa.