Instancetype and ID keywords in objective-C

Source: Internet
Author: User

Conversion from: instancetype and ID keyword in objective-C

I. What is instancetype?

The same as the ID indicates an unknown object.

 

2. Related Result types)

According to the naming rules of cocoa, the following rules are met:

1. In the class method, it must start with alloc or new

2. The instance method starts with autorelease, init, retain, or self.

Returns an object of the class type of a method. These methods are called associated return types. In other words, the returned results of these methods are of the type of the class where the method is located. This is a bit of a detour. Please refer to the following example:

@interface NSObject+ (id)alloc;- (id)init;@end@interface NSArray : NSObject@end

 

When we initialize nsarray using the following method:

NSArray *array = [[NSArray alloc] init];

According to cocoa naming rules and statements[Nsarray alloc]Is nsarray * because the alloc return type belongs to the associated return type. Similarly,[[Nsarray alloc] init]The returned result is also nsarray *.

 

Iii. Role of instancetype 1. Role

If a method is not associated with the return type, it is as follows:

@interface NSArray+ (id)constructAnArray;@end

 

When we initialize nsarray using the following method:

[NSArray constructAnArray];

 

According to the cocoa method naming convention, the returned type is the same as the returned type of the method declaration, which is ID.

However, if instancetype is used as the return type, it is as follows:

@interface NSArray+ (instancetype)constructAnArray;@end

 

When initializing nsarray in the same way:

[NSArray constructAnArray];

 

The returned type is the same as the type of the class where the method is located. It is nsarray *!

To sum up, the role of instancetype is to make the types of classes returned by methods of non-correlated return types!

2. Benefits

The ability to determine the object type can help the compiler better locate code writing problems for us, such:

[[[NSArray alloc] init] mediaPlaybackAllowsAirPlay]; //  "No visible @interface for `NSArray` declares the selector `mediaPlaybackAllowsAirPlay`"[[NSArray array] mediaPlaybackAllowsAirPlay]; // (No error)

 

In the first line of code in the preceding example, the result of [[nsarray alloc] init] Is nsarray *, so that the compiler can detect whether nsarray implements the mediaplaybackallowsairplay Method Based on the returned data type. This helps developers discover errors during compilation.

The second line of code, because array does not belong to the associated return type method, [nsarray array] returns the ID type. The Compiler does not know whether the ID type object has implemented the mediaplaybackallowsairplay method, in this way, you cannot promptly discover Errors for developers.

 

Iv. Similarities and Differences between instancetype and Id 1. Similarities

Can be used as the return type of the method.

2. Differences

① Instancetype can return objects of the same type as the class of the method. ID can only return unknown objects;

② Instancetype can only be used as the return value, not as the parameter as the ID, for example, the following statement:

//err,expected a type- (void)setValue:(instancetype)value{    //do something}

 

It is wrong. It should be written:

- (void)setValue:(id)value{    //do something}

 

 

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.