In Objective-C, what\'s the difference between declaring a variable id
versus declaring it NSObject *
?
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];
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.
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.)
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.