1. ID and NSObject *(1) ID is an Objective-c object, but not necessarily a NSObject object, not all Foundation/cocoa objects are inherited from NSObject objects, such as Nsproxy. At the same time, there are a lot of common methods between ID and NSObject object, such as retain and release method. In one step: All objects are essentially ID types. (2) for ID, you can call any visible selector, the compiler and IDE do not do type checking, this time you need to do type check and type conversion, to ensure that these calls do not go wrong. For NSObject * types, only the selector declared by the NSObject object can be called, and the selector of its subclasses cannot be called, and the compiler checks. (3) You can use the ID for some places where you do not want to or cannot do a type check. For example, in a collection (array, collection) type, such as in some places where you do not know the return type of the method (such as Alloc), for example, we often declare delegate as the ID type, and then use Respondtoselector when running: To check it out. 2. Id<nsobject> uses id<nsobject> to declare an object, which is equivalent to telling the compiler that we don't know the type of the object, but it implements NSObject protocol. A pointer of this type, which can be used to point to a nsobject* object or to a Nsproxy* object, because the NSObject object and the Nsproxy object are now NSObject protocol. 3. ID and Instancetype in the case of instancetype effective, should try to use Instancetype. As for what is appropriate, refer to stack overflow above: "Use instancetype whenever it's appropriate, which is whenever a class returns an Instan Ce of that same class. ", http://stackoverflow.com/questions/8972221/ Would-it-be-beneficial-to-begin-using-instancetype-instead-of-id/14652187#14652187 apple officialDocumentation: in Your code, replace occurrences of
id
As a return value with
instancetype
Where appropriate. This is typically
init
Methods and class factory methods. Even though the compiler automatically converts methods that begin with "alloc," "Init," or "new" and has a return type O F
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 Reference
- Http://unixjunkie.blogspot.hk/2008/03/id-vs-nsobject-vs-id.html
- Http://stackoverflow.com/questions/8972221/would-it-be-beneficial-to-begin-using-instancetype-instead-of-id
- Http://stackoverflow.com/questions/466777/whats-the-difference-between-declaring-a-variable-id-and-nsobject
- Http://stackoverflow.com/questions/7903954/why-use-id-when-we-can-just-use-nsobject
- http://blog.csdn.net/happytengfei/article/details/11473931
ID, NSObject *, id<nsobject>, instancetype