For objective-c beginners, using instancetype and ID to define a simple constructor, exactly what is the difference?

Source: Internet
Author: User

After touching the OC language for some time, we all know that an object of unknown type can be represented by an ID, so what is instancetype for?

The role of 1.instancetype

is to make the methods of the non-associative return type return the type of the class

2. Benefits of using Instancetype

A. The ability to determine the type of object can help the compiler to better locate code for us to write problems

B. The compiler is able to detect whether a method is implemented based on the returned data type, enabling developers to find errors during the compile phase.

The similarities and differences between 3.instancetype and ID

A. The same point

Can be used as the return type of a method

B. Different points①instancetype can return objects of the same type as the method's class, and the ID can only return objects of unknown type;

②instancetype can only be used as a return value, not as a parameter as an ID

3. Summary : Although using instancetype instead of ID as the return value does not have a technical benefit, this is because the compiler automatically converts the ID into instancetype. The beginner will assume that the value type returned by INIT is an ID, but the compiler returns INSTANCETYPE.

Finally, borrowing the nine words summed up by others to summarize all the above conclusions, using Instancetype has three advantages: explicit programmatic consistency.

For objective-c beginners, using instancetype and ID to define a simple constructor, exactly what is the difference?

Related Article

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.