115

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?

3
  • 34
    It's the opposite of the ego. For some people that's void*
    – Hot Licks
    Nov 2, 2011 at 20:46
  • Here is a discussion relevant to the question: SO question
    – dmitri
    Jul 21, 2015 at 4:12
  • 1
    Maybe you're here because nobody told you about iD
    – jrh
    Feb 10, 2021 at 21:59

5 Answers 5

137

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.

4
  • 14
    Supplement: id 's definition is typedef struct objc_object *id; , so it pointer to a Objc object.
    – zhongwuzw
    Jan 9, 2018 at 14:56
  • 2
    is it the same as NSObject? means, can i replace id with NSObject when it's a return type? Jun 15, 2018 at 18:50
  • @ThunderWiring - A precise answer would be NO, since you could assign an NSProxy to a variable typed as an id. (NSProxy does not descend from NSObject.)
    – Mario
    Jun 4, 2021 at 17:03
35

id is a pointer to any Objective-C object (objc_object). It is not just a void pointer and you should not treat it as so. It references an object that should have a valid isa pointer. The values that can be stored in id are also not just limited to NSObject and its descendants, which starts to make sense of the existence of the NSObject protocol as well as the NSProxy class which does not even inherit from NSObject. The compiler will allow you to assign an object referenced by type id to any object type, assign any object type to id, as well as send it any message (that the compiler has seen) without warning.

31

id is a generic type. This means that the compiler will expect any object type there, and will not enforce restrictions. It can be useful if you're expecting to use more than one class of objects there; you can then use introspection to find out which class it is. id automatically assumes a pointer, as all objects in Objective-C are passed as pointers/references.

Some Additional Resources:
id vs NSObject vs id*
Objective-C Programming (Wikibooks)
Introspection
Dynamic Typing

3
  • 2
    The first blog link is now dead, but can be found at archive.org, the last two links now seem to redirect to a suspicious looking site, please replace them with archive.org links: Introspection, Dynamic Typing
    – jrh
    Jan 27, 2021 at 22:14
  • 1
    Last two links were to my old blog … I used to have the domain for it 😅 Just updated the links to their current Wordpress subdomain
    – FeifanZ
    Jan 29, 2021 at 3:13
  • Thanks for the update, can you edit the first link as well? It's still dead. An archive.org link is better than nothing, I guess.
    – jrh
    Jan 29, 2021 at 3:17
9
  • id is a data type of object identifiers in Objective-C, which can be use for an object of any type no matter what class does it have. id is the final super type of all objects.

In java or c# we use like this

 Object data = someValue;


 String name =(Object)data;

but in objective c

id data= someValue;



NSString *name= data;
6

Yes and no. It's true that having id x designates x as a pointer, but saying that the pointer type conversion rules apply is wrong, because "id" has special type conversion rules. For example, with a void * pointer you can't do this:

void *x;
char *y = x; // error, this needs an explicit cast

On the contrary, it's possible with id:

id x;
NSString *y = x;

See more usage of type id in objective c examples.

In addition in the "modern" Objective C it's preferred to use instancetype instead of "id" on "init" methods. There's even an automatic conversion tool in Xcode for changing that. Read about instancetype: Would it be beneficial to begin using instancetype instead of id?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.