Instancetype and ID differences in OBJECTIVE-C

Source: Internet
Author: User

In the daily coding process, we almost developed the habit of using ID for all the return values of the indeterminate type. Indeed, Because it has a general-purpose pointer feature of the balm, plus instancetype after ios7.0. Many people have not changed their coding habits. Let alone dig deep into the nuances of the two. In fact, both are in the type representation, Can represent any object type. But one thing we need to note is that instancetype can only be used in return value types, and the ID is not only used for return value types, but also for parameter types.

In use, Instancetype has one more advantage over ID, which is that the compiler detects the true type of instancetype. In peacetime, we may not be aware of this, but in practical development, this little bit of error may kill you.

first, what is Instancetype

Instancetype is clang 3.5, a keyword provided by clang that represents an unknown type of objective-c object returned by a method. We all know that an object of an unknown type can be represented by an ID keyword, so why is there another instancetype? Look down.

second, the associated return type ( related result Types )

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

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

2. In the instance method, start with autorelease,init,retain or self

Returns an object of the class type of a method, known as a method that associates a return type. In other words, the return result of these methods is the type of the class in which the method is located, which is a bit around the mouth, see the following example:

[OBJC] view plain copy

    1. @interface NSObject
    2. + (ID) alloc;
    3. -(ID) init;
    4. @end
    5. @interface Nsarray:nsobject
    6. @end

When we initialize Nsarray using the following method:

[OBJC] view plain copy

    1. nsarray *array = [[Nsarray alloc] init];

According to the Cocoa naming convention, the type of statement is because the return type of Alloc belongs to the associated return type.

Third, Instancetype function

1 , Role

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

[OBJC] view plain copy

    1. @interface Nsarray
    2. + (ID) Constructanarray;
    3. @end

When we initialize Nsarray using the following method:

[OBJC] view plain copy

    1. [Nsarray Constructanarray];

According to the cocoa Method naming specification, the resulting return type is the same as the return type of the method declaration, which is the ID.

However, if you use Instancetype as the return type, as follows:

[OBJC] view plain copy

    1. @interface Nsarray
    2. + (instancetype) Constructanarray;
    3. @end

When initializing Nsarray in the same way:

[OBJC] view plain copy

    1. [Nsarray Constructanarray];

The resulting return type and method are the same type as the class, which is nsarray*!

To summarize, the Instancetype function is to make the methods of the non-associative return type return the type of the class!

2 , Benefits

Being able to determine the type of an object can help the compiler better position code for us, such as:

[OBJC] view plain copy

    1. [[[Nsarray alloc] init] mediaplaybackallowsairplay]; "No visible @interface for ' Nsarray ' declares the selector ' Mediaplaybackallowsairplay '"
    2. [[Nsarray array] mediaplaybackallowsairplay]; (No error)

The first line of code in the previous example, because the result of [[Nsarray Alloc]init] is nsarray*, allows the compiler to detect whether Nsarray implements the Mediaplaybackallowsairplay method based on the returned data type. Helps developers find errors during the compile phase.

The second line of code, because the array does not belong to the associated return type method, [Nsarray array] Returns the ID type, the compiler does not know whether an object of the ID type implements the Mediaplaybackallowsairplay method, and it is not possible for developers to find errors in time.

Four, Instancetype and the ID Similarities and differences

1 , the same point

Can be used as the return type of a method

2 , 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 like an ID, as in the following notation:

[OBJC] view plain copy

    1. Err,expected a type
    2. -(void) SetValue: (instancetype) value
    3. {
    4. Do something
    5. }

Is wrong and should be written as:

[OBJC] view plain copy

    1. -(void) SetValue: (ID) value
    2. {
    3. Do something
    4. }

V. References

1, http://nshipster.com/instancetype/

2, Http://clang.llvm.org/docs/LanguageExtensions.html#objective-c-features

3, http://blog.sina.com.cn/s/blog_1512e78160102vtfy.html

Six, What are the benefits of using Instancetype instead of ID for return type?

There are three advantages to using Instancetype:

1. Clear sex. The code does only what you let it do, not the other.

2, stylized. You will develop good habits that will be useful at some point and certainly have a niche.

3, consistency. Make your code more readable.

Clear Sex

There is really no technical benefit to using INSTANCETYPE instead of ID as the return value. But this is because the compiler automatically converts the ID into instancetype. You think the value type returned by INIT is an ID, but the compiler returns INSTANCETYPE.

These two lines of code are the same for the compiler:

-(ID) Initwithbar: (Nsinteger) bar;

-(Instancetype) Initwithbar: (Nsinteger) bar;

But in your eyes, these two lines of code are different. You shouldn't learn to ignore it.

Pattern

There is no difference when using methods such as INIT, but there is a difference when defining a simple constructor.

These two lines of code are not equivalent:

+ (ID) Foowithbar: (Nsinteger) bar;

+ (Instancetype) Foowithbar: (Nsinteger) bar;

If you use Instancetype as the return type of a function, there is no error.

Consistency:

Finally, imagine putting everything together: you want an init method and a simple constructor.

If you use an ID as the return type of the Init function, the final code is as follows:

-(ID) Initwithbar: (Nsinteger) bar;

+ (Instancetype) Foowithbar: (Nsinteger) bar;

But if you use Instancetype, the code is as follows:

-(Instancetype) Initwithbar: (Nsinteger) bar;

+ (Instancetype) Foowithbar: (Nsinteger) bar;

The code is more consistent and more readable. They return the same thing at a glance.

Vii. Personal Conclusions: This means that using instancetype to return an instance of the method is called, and the ID is not necessarily, because the ID is used as a paradigm.

Before writing a message that returns an ID, ask yourself: Does this class return an instance? If returned, use Instancetype.

There must be a time to return the ID, but you should use instancetype more often.

Instancetype and ID differences in OBJECTIVE-C

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.