tags:

views:

494

answers:

4

This question is out of pure curiosity. How does Cocoa define the id type? Is it just a typedef for a void *? Also, if you know which header file it is defined in, I would be interested in taking a look.

+11  A: 

Hold down the command key and double click on any highlighted term to jump to its definition.

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


typedef struct objc_selector    *SEL;    
typedef id          (*IMP)(id, SEL, ...); 
Azeem.Butt
+1 cmd+double click is wonderfully useful!
Dave DeLong
+1 Great answer. I hope to work more with XCode in the next year, and the keyboard shortcut will be super helpful to know.
Doug Neiner
+1 over even my own answer. I already knew this, but it is just too damn useful.
Mike
opt-double-click will bring up the documentation, if any is available, too!
bbum
+5  A: 

in objc.h

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

To find out on your own, in XCode, right click id -- or any other type -- and select Jump to definition. It's interesting to note the similarities to other C/C++ based object systems; an object pointer -- an id -- points to a struct that starts with a point to shared class information. I many C++ implementations, that would be the virtual function table, as it would be with Microsoft's COM. In Cocoa, the particulars of objc_class aren't revealed to us in the header file.

Oren Trutner
+2  A: 

The id type is generally declared like:

typedef struct objc_object *id;

This is critical for Objective-C++ where the type is part of a mangled function name.

You can take a look in /usr/include/objc/objc.h

caleb
+5  A: 

It is delcared in /usr/include/objc/objc.h (on Leopard) as follows:

typedef struct objc_object {
    Class isa;
} *id;

Which means it is not void * at all, but rather a pointer to a struct that contains a single member, pointing at the class definition. Interesting indeed.

I remember when I was just getting into C and learning that Objective-C was initially implemented as just a preprocessor layer on top of C. It isn't quite like that anymore.

The best reading on the topic I've found:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html

Mike