id、NSObject *、id<NSObject>、instancetype的区别

1. id 与 NSObject *
(1) id 是 Objective-C 对象,但是并不一定是NSObject对象,并非所有的Foundation/Cocoa对象都是继承于NSObject对象的,比如NSProxy。同时,id与NSObject对象之间有很多的共同方法,比如retain与release等方法。更一步来说:所有的对象本质来说都是 id 类型的。
(2) 对于id来说,你可以调用任意可见的selector,编译器和IDE不会进行类型检查,这个时候就需要你自己进行类型检查并且进行类型转换,来确保这些调用不会出错。而对于NSObject *类型,只能调用NSObject对象所声明的selector,不能调用它子类的selector,编译器会进行检查。
(3) 对于一些不想或者不能进行类型检查的地方,可以使用id。比如在集合(array, collection)类型中,比如在一些你并不知道方法的返回类型的地方(比如alloc),比如我们经常声明delegate为id类型,在运行的时候再使用respondToSelector:来进行检查。
 
2. id<NSObject>
使用id<NSObject>来声明一个对象,相当于告诉编译我们并不知道这个对象的类型,但是它实现NSObject protocol。一个这种类型的指针,即可以用来指向NSObject*对象,也可以用来指向NSProxy*对象,因为NSObject对象与NSProxy对象都是现了NSObject protocol。
 
3. id 与 instancetype
在instancetype有效的情况下,应该尽量去使用instancetype。至于什么是合适的时候,可以参考stack overflow上面所说:“Use instancetype whenever it's appropriate, which is whenever a class returns an instance of that same class.”, http://stackoverflow.com/questions/8972221/would-it-be-beneficial-to-begin-using-instancetype-instead-of-id/14652187#14652187
 
Apple官方文档:
 
 
In your code, replace occurrences of  id as a return value with  instancetype where appropriate. This is typically the case for  init methods and class factory methods. Even though the compiler automatically converts methods that begin with “alloc,” “init,” or “new” and have a return type of  id to return  instancetype, it doesn’t convert other methods.  Objective-C convention is to write instancetype explicitly for all methods.
 
         Emphasis mine. Source:  Adopting Modern Objective-C

猜你喜欢

转载自www.linuxidc.com/Linux/2015-11/124743.htm