< Objective-c >id and Instancetype

Source: Internet
Author: User

Same point:

Can be used as the return type of a method

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 of a method as an ID

Benefits:

The ability to determine the type of object that helps the compiler to better locate code for us to write problems

[[[Nsarray alloc] init] mediaplaybackallowsairplay];

The return type is instancetype: Because the return value of [[Nsarray Alloc]init] is nsarray*, This allows the compiler to detect whether the Nsarray implements the Mediaplaybackallowsairplay method based on the returned data type. Helps developers find errors during the compile phase.

[[Nsarray array] mediaplaybackallowsairplay];

The return type Id:[nsarray array] Returns the ID type, and the compiler does not know whether an object of the ID type implements the Mediaplaybackallowsairplay method, nor is it able to detect errors for developers in a timely manner.

< Objective-c >id and Instancetype

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.