简体   繁体   中英

What's the difference between declaring a variable “id” and “NSObject *”?

在Objective-C中,声明变量id与声明NSObject *之间的区别是什么?

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.

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.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM