In Objective-C, what's the difference between declaring a variable id
versus declaring it NSObject *
?

- 54,380
- 8
- 73
- 102

- 2,279
- 2
- 18
- 22
-
NSObject contain isa pointer , id doesn't. Have a look at http://stackoverflow.com/a/19634973/944634 – Parag Bafna Oct 28 '13 at 13:05
4 Answers
With a variable typed id
, you can send it any known message and the compiler will not complain. With a variable typed NSObject *
, you can only send it messages declared by NSObject (not methods of any subclass) or else it will generate a warning. In general, id
is what you want.
Further explanation: All objects are essentially of type id
. The point of declaring a static type is to tell the compiler, "Assume that this object is a member of this class." So if you send it a message that the class doesn't declare, the compiler can tell you, "Wait, that object isn't supposed to get that message!" Also, if two classes have methods with the same name but different signatures (that is, argument or return types), it can guess which method you mean by the class you've declared for the variable. If it's declared as id
, the compiler will just throw its hands up and tell you, "OK, I don't have enough information here. I'm picking a method signature at random." (This generally won't be helped by declaring NSObject*
, though. Usually the conflict is between two more specific classes.)

- 234,037
- 30
- 302
- 389
-
2Great explanation, though I hesitate to say that "In general, id is what you want". Although "id" is quite flexible due to the dynamic typing, it also provides virtually no warnings, so if you call an unsupported method, what could be caught at compile time becomes a runtime problem. Static typing (with MyClassName* etc.) when used judiciously can make life much simpler, particularly when debugging in Xcode (it can show a more intelligent summary of the object) or catching method calls with misspelled or incomplete selectors. – Quinn Taylor Jun 25 '09 at 07:28
-
The distinction I meant to draw is specifically between id and NSObject*, not static typing in general. It's much more common that you'd want to use id than statically type as NSObject. – Chuck Jun 25 '09 at 14:57
-
-
@Chuck right, sorry, it must have been too late to think... or too early – Daniel Nov 20 '14 at 08:15
id
means "an object", NSObject *
means "an instance of NSObject
or one of its subclasses". There are objects in Objective-C which are not NSObject
s (the ones you'll meet in Cocoa at the moment are NSProxy
, Protocol
and Class
). If some code expects an object of a particular class, declaring that helps the compiler check that you're using it properly. If you really can take "any object" - for instance you are declaring a delegate and will test all method sends with respondsToSelector:
calls - you can use an id
.
Another way to declare an object variable is like "id <NSObject>
", which means "any object which implements the NSObject
protocol.
-
NSProxy and Protocol are classes. "Class" is not a class, but simply a type that can refer to pointers to class objects. Don't lump them together – user102008 Jul 26 '11 at 01:44
-
@user102008: you can send messages to a Class, which makes it an object in the world of objc. – Jul 26 '11 at 08:05
-
yes, class objects are objects, but they are not of a class called "Class" – user102008 Jul 26 '11 at 19:38
From my limited understanding of Objective-C, not all objects are derived from NSObject (unlike Java where all objects derive from Object). You can theoretically have other root objects. id could apply to any of those non-NSObject derived objects.

- 17,592
- 12
- 63
- 89
I would like to add another difference. When you add a protocol to id
, it does not longer mean that it will be of type NSObject *
, it just means that it will be any class that confirms to that protocol.
So, for example, this code will not throw any error, since NSObject
's category NSDelayedPerforming
has that method:
id testId;
[testId performSelector:@selector(isKindOfClass:) withObject:[NSObject class] afterDelay:.5];
However, this code will show the error No known instance method for selector "performSelector:withObject:afterDelay:"
:
id<NSMutableCopying> testId;
[testId performSelector:@selector(isKindOfClass:) withObject:[NSObject class] afterDelay:.5];

- 20,420
- 10
- 92
- 149