What is the difference between ID, NSObject *, ID, instancetype in iOS development?

Source: Internet
Author: User


When using objective-c language for iOS app development, it often involves the use of the four concepts of ID, NSObject *, ID, instancetype, but these four are also the most confusing content for iOS beginners, and the following is a small compilation of some iOS development tutorials and documentation , collecting their different usage, share to everybody, come together to see.

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, 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

Using an ID to declare an object is equivalent to telling the compiler that we do not 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, the instancetype should be used as much as possible. As for what is appropriate, refer to stack overflow above: "Use instancetype whenever it's appropriate, which is whenever a

Class returns an instance of that same class. ",

Apple Official Documentation:

In your code,replace occurrences of the ID as a return value with Instancetype whereappropriate. This is typically, the case for INIT methods and class Factorymethods. Even though the compiler automatically converts methods that Beginwith "Alloc," "Init," or "new" and has a return type of ID to Returninstancetype, it doesn ' t convert other methods. Objective-c Convention is Towrite Instancetype explicitly for all methods.

Through the above introduction, I hope you will use ID, NSObject *, ID, instancetype help.

Related article: "Objective-c development language function writing tips"



Text/wheat and wheat (Jane book author)
Original link: http://www.jianshu.com/p/92910741d0d5
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

What is the difference between ID, NSObject *, ID, instancetype in iOS development?

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.