The difference between IOS Instancetype and ID

Source: Internet
Author: User

Difference:

1>instancetype on a type representation, as with an ID, can represent any object type

2>instancetype can only be used on return value types and cannot be used as an ID on parameter types

One benefit of 3>instancetype than ID: The compiler detects the true type of instancetype

The 3rd explanation: in the following case///In the person.m file+ (ID) person{return [[Self alloc] init];}///MAINVIEWCONTROLLER.M, the following line of code, pointing to the object of the person class with a pointer to a string type, compiles through because the ID type returned by person, any pointer can point to itNSString *str = [person person];//If you use Instancetype, there will be a warning when compiling. That means Instancetype is more than an ID. Detect real-world functionality, exposing the risk of a program in advance+ (Instancetype) person{return [[Self alloc] init];} NSString *str = [person person];//There will be a warning, the person type returned by [person person], warning message: Assigning data of type person to string type //So to speak, wherever you can use the ID, it is recommended to change to instancetype, for example, the following code-(Instancetype) Initwithdic: (Nsdictionary *) app
{
if (self = = [Super init]) {
Self.name = app[@ "name"];
Self.icon = app[@ "icon"];
}
return self;
}
+ (Instancetype) Initwithdic: (Nsdictionary *) app
{
return [[[Self alloc]init] initwithdic:app];}

The difference between IOS Instancetype and ID

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.