Why you need to use INSTANCETYPE instead of ID

Source: Internet
Author: User

Four years ago Clang added keyword Instancetype to replace the return type IDs of methods such as-alloc and-init, so where is the use of instancetype better than the ID?

Instancetype Declaration

Whenever a class returns its same class instance, it is more appropriate to use instancetype.

We know that when calling the-alloc,-init, or class factory (+) method of a class (or subclass), the Instancetype keyword returns its class instance. In this case, there are 100 benefits of using Instancetype instead of ID.


Apple also said:

In your code, replace occurrences of the ID as a return value with Instancetype where appropriate. This is typically, the case for INIT methods and class factory methods. Even though the compiler automatically converts methods that begin with "alloc," "Init," or "new" and has a return type O F ID to return instancetype, it doesn "t convert other methods. OBJECTIVE-C convention is to write instancetype explicitly for all methods.

More compelling evidence: adopting modern objective-c

Why?

First you may need to know what the compiler will do.

@interface Foo:nsobject

Initializer

-(ID) Initwithbar: (Nsinteger) bar;

Convenience constructor

+ (ID) Foowithbar: (Nsinteger) bar;

@end

For convenience constructor, you have to use instancetype because the compiler does not automatically convert the ID to instancetype.

And for initializer, when your code is like this:

Initializer

-(ID) Initwithbar: (Nsinteger) bar;

Under Arc, the compiler will turn it into this:

Initializer

-(Instancetype) Initwithbar: (Nsinteger) bar;

That's why it's not necessary to have someone tell you to use instancetype, but I suggest you use Instancetype for three reasons:

    1. Clarity: Your code has a clear meaning and will never do anything else.

    2. Stereotypes: You build a good habit, which is really important.

    3. Consistency: make your code more consistent and more readable.

1. Clear Sex

Returning Instancetype in the-init method does not have much benefit, because the compiler automatically converts the ID to instancetype.

This is equivalent to the compiler:

-(ID) Initwithbar: (Nsinteger) bar;

-(Instancetype) Initwithbar: (Nsinteger) bar;

But at least it's different in your eyes.

2. Modelling

Although there is no difference between the return ID and the instancetype for Init or other methods, they are different in the convenient constructor (+) method:

+ (ID) Foowithbar: (Nsinteger) bar;

+ (Instancetype) Foowithbar: (Nsinteger) bar;

You have to use the second one to ensure that you are running the correct results every time.
Consider a situation like this:


Then we call it this way:

A compile warning was found for X because our +factorymethoda returned instancetype, which represents myobject* because MyObject has no -count method, so a compile warning appears.

However, because our +FACTORYMETHODB returns an ID, the compiler does not give a warning on line y, because the OBJC language dynamic type and dynamically bound attributes, the ID may be any class, and because the -count method may exist somewhere in a class, So for the compiler, what the +factorymethodb method returns is the possibility of implementing the-count method.

3. Consistency

The ID can be returned in Init, so you might write:

-(ID) Initwithbar: (Nsinteger) bar;

+ (Instancetype) Foowithbar: (Nsinteger) bar;

But if you use instancetype, the result will be this:

-(Instancetype) Initwithbar: (Nsinteger) bar;

+ (Instancetype) Foowithbar: (Nsinteger) bar;

This looks more consistent and more readable. They return the same thing, but the latter looks more obvious, doesn't it?

Conclusion

When you want to return an ID type, you should think about whether it will return an instance of a class, and if so, use Instancetype.

Why you need to use INSTANCETYPE instead of 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.