ID, NSObject *, id<nsobject>, instancetype

Source: Internet
Author: User

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  idAs a return value with instancetypeWhere appropriate. This is typically initMethods 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 idTo return instancetype, it doesn ' t convert other methods. OBJECTIVE-C convention is to write instancetypeExplicitly 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.