What are the benefits of [IOS] using instancetype instead of IDs for return types? Go

Source: Internet
Author: User

Copyright belongs to the author.
Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Management policy
Links: http://zhuanlan.zhihu.com/Foundation/19569459
Source: Know

2014-07-07 Update: Apple fully uses instancetype instead of ID in iOS 8


Steven Fisher: It's good to use instancetype as long as a class returns its own instance.


@interface Foo:NSObject - (id)initWithBar:(NSInteger)bar; // initializer + (id)fooWithBar:(NSInteger)bar; // convenience constructor@end

For simple constructors (convenience constructor), you should always use Instancetype. The compiler does not automatically convert the ID to instancetype. The ID is a generic object, but if you use Instancetype, the compiler knows what type of object the method returns.


This problem is not only academic, for example, [[[Nsfilehandle Filehandlewithstandardoutput] writedata:formatteddata] in Mac OS X (only in this OS version) will be an error " Multiple methods named ' WriteData: ' found with mismatched result, parameter type or attributes. " The reason is that both Nsfilehandle and Nsurlhandle provide WriteData: methods. Because the type returned by [Nsfilehandle Filehandlewithstandardoutput] is an ID, the compiler does not determine which class is requested by the WriteData: method.


You can use

[(NSFileHandle *)[NSFileHandle fileHandleWithStandardOutput] writeData:formattedData];

Or

NSFileHandle *fileHandle = [NSFileHandle fileHandleWithStandardOutput];[fileHandle writeData:formattedData];

To get around.


The better way, of course, is to declare that the return type of Filehandlewithstandardoutput is instancetype. (Note: This sample code does not report an error in iOS because only Nsfilehandle in iOS provides the WriteData: method.) But length and so on, Uilayoutsupport will return cgfloat, and NSString will return Nsuinteger)


The initializer situation is more complicated when you enter

- (id)initWithBar:(NSInteger)bar

The compiler will assume that you entered the

- (instancetype)initWithBar:(NSInteger)bar

For arc, this is necessary. The related result type of Clang Language extensions (related result types) also mentions this. Maybe others will tell you that you don't have to use instancetype, but I suggest you use it. Here's why I suggested so.


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.


Conclusion


Unless you intentionally write code for the old compiler, you should use instancetype when appropriate.


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.


This answer was last edited on July 12, 2013


Original post: Objective c-would It is beneficial to begin using Instancetype instead of ID?

Note: I am also just getting started, if the translated terms are inconsistent with the prevailing terminology, please let us know if the translated content is incorrect or outdated, thank you.

What are the benefits of [IOS] using instancetype instead of IDs for return types? Go

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.