views:

87

answers:

3

If I'm using Objective-C, here's how I declare an initialize an int:

int a = 1;

vs an object:

myObj *a = [[myObj alloc] init];

So this is a pointer to an object as denoted by the '*'. My question is, why aren't objects of type id declared this way? I.e., why is it this:

id genericObj;

and not:

id *genericObj;

?

+4  A: 

Because id means identifier. Identifier, like pointer, identifies the object. Identifier isn't the object itself.

You can always treat it as typedef <some-mysterious-root-type>* id if you want.

Pavel Minaev
+10  A: 

Because id is defined as:

typedef struct objc_object {
    Class isa;
} *id;

So it's already a pointer.

rein
Yep, and you can easily find this out by command-double clicking on the word `id` in your code, which will take you to the definition.
Rob Keniger
+2  A: 

Pavel's answer is correct. Specifically, the "<mysterious-root-type> " is declared in objc.h as:

typedef struct objc_class *Class;
typedef struct objc_object {
    Class isa;
 } *id;
Jarret Hardie