Analysis of iOS-instancetype

Source: Internet
Author: User

Analysis of iOS-instancetype

OC is a rapidly developing language, such as ARC, object literals, subscripting, blocks, and Auto Synthesis. Let's see its amazing changes. Instancetype is a keyword provided by clang starting with clang3.5, indicating an unknown OC object returned by a method. Before that, we know that we can use id to represent unknown return types. Why should we use instancetype?

[Associated return type]

According to the naming rules of Cocoa, the methods that meet the following rules will return an object of the class type of the method, these methods are called associated return type methods. In other words, the returned results of these methods are of the type of the class where the method is located.

1. The class method starts with alloc or new;

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

For example:

 

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

During initialization:
NSArray *array = [[NSArray alloc] init];

According to the Cocoa naming rules, the return type of the statement [NSArray alloc] Is NSArray *, because the return type of alloc belongs to the associated return type. Similarly, the returned results of [[NSArray alloc] init] are also NSArray *.

 

 

[Function]

If a class method is not associated with the return type,

 

    @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.

 

 

But what if we use instancetype as the return type?

 

@interface NSArray  + (instancetype)constructAnArray;  @end 

Use the same method for initialization. The returned type is the same as the type of the class where the method is located, which is NSArray *.

 

Therefore, to sum up, the role of instancetype is to make the types of classes returned by non-correlated methods.

 

[Similarities and differences between instancetype and id]

Similarities: both can be used as the return type of the method.

Differences:

(1) instancetype can return objects of the same type in the class where the method is located. IDS can only return unknown objects;

(2) instancetype can only be used as the return value, and cannot be used as a parameter like id;

 

 

Note:

1. There is no difference between id and instancetype for the init method. Because the compiler will optimize the id to instancetype. When it is clear that the returned type is the current class, using instancetype can avoid uncompiled errors caused by id.

 

 

 

 

 

 

 

 

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.