Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

ios - Why is instancetype used?

Can someone please explain to me (in simple terms) why an instancetype is used in Objective-C?

- (instancetype) init { 
    self = [super init];
    if (self) { 
        // Custom initialization
    } 
    return self; 
} 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's to increase type safety.

Back in the old days, initialisers just returned an object of type id (any object).

With normal initialisers (those that begin with "init", "alloc" or "new"), this wasn't usually a problem. The compiler would automatically infer the type that it returned and therefore restrict any method calls on the object to the instance methods of that class.

However, this was a problem with static convenience initialisers or "factory methods" that didn't necessarily follow the same naming convention - therefore it was unable to apply the same type safety.

This means that with a class like this:

@interface Foo : NSObject

+(id) aConvenienceInit;

@end

The compiler would accept code like this:

NSArray* subviews = [Foo aConvenienceInit].subviews;

Why? Because the returned object could be any object, so if you try and access a UIView property - there's no type safety to stop you.

However, now with instancetype, the result you get back is of type of your given instance. Now with this code:

@interface Foo : NSObject

+(instancetype) aConvenienceInit;

@end

...

NSArray* subviews = [Foo aConvenienceInit].subviews;

You'll get a compiler warning saying that the property subviews is not a member of Foo*:

Although it's worth noting that the compiler will automatically convert the return type from id to instancetype if your method begins with "alloc", "init" or "new" - but nonetheless using instancetype wherever you can is a good habit to get into.


See the Apple docs on instancetype for more info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...