Objective-C中的instancetype和id区别

来源:互联网 发布:json在线压缩工具 编辑:程序博客网 时间:2024/06/09 00:59

Related result types¶

According to Cocoa conventions, Objective-C methods with certain names (“init”, “alloc”, etc.) always return objects that are an instance of the receiving class’s type. Such methods are said to have a “related result type”, meaning that a message send to one of these methods will have the same static type as an instance of the receiver class. For example, given the following classes:

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

and this common initialization pattern

NSArray *array = [[NSArray alloc] init];

the type of the expression [NSArray alloc] is NSArray* because alloc implicitly has a related result type. Similarly, the type of the expression [[NSArray alloc]init] is NSArray*, since init has a related result type and its receiver is known to have the type NSArray *. If neither alloc nor init had a related result type, the expressions would have had type id, as declared in the method signature.

A method with a related result type can be declared by using the type instancetype as its result type.  instancetype is a contextual keyword that is only permitted in the result type of an Objective-C method, e.g.

@interface A+ (instancetype)constructAnA;@end

The related result type can also be inferred for some methods. To determine whether a method has an inferred related result type, the first word in the camel-case selector (e.g., “init” in “initWithObjects”) is considered, and the method will have a related result type if its return type is compatible with the type of its class and if:

  • the first word is “alloc” or “new”, and the method is a class method, or
  • the first word is “autorelease”, “init”, “retain”, or “self”, and the method is an instance method.

If a method with a related result type is overridden by a subclass method, the subclass method must also return a type that is compatible with the subclass type. For example:

@interface NSString : NSObject- (NSUnrelated *)init; // incorrect usage: NSUnrelated is not NSString or a superclass of NSString@end

Related result types only affect the type of a message send or property access via the given method. In all other respects, a method with a related result type is treated the same way as method that returns id.

Use __has_feature(objc_instancetype) to determine whether the instancetype contextual keyword is available.


使用instancetype有三点好处:

1、明确性。代码只做你让它做的事,而不是其他。

2、程式化。你会养成好习惯,这些习惯在某些时候会很有用,而且肯定有用武之地。

3、一致性。让代码可读性更好。

明确性

用instancetype代替id作为返回值的确没有技术上的好处。但这是因为编译器自动将id转化成了instancetype。你以为init返回的值类型是id,其实编译器返回了instancetype。

这两行代码对于编译器来说是一样的:

- (id)initWithBar:(NSInteger)bar;- (instancetype)initWithBar:(NSInteger)bar;

但在你眼里,这两行代码却不同。你不该学着忽视它。

模式化

在使用init等方法时的确没有区别,但在定义简易构造函数时就区别了。

这两行代码并不等价:

+ (id)fooWithBar:(NSInteger)bar;+ (instancetype)fooWithBar:(NSInteger)bar;

如果用instancetype作为函数的返回类型,就不会出错。

一致性:

最后,想象把所有东西放到一起时的情景:你想要一个init方法和一个简易构造函数。

如果你用id来作为init函数的返回类型,最终代码如下:

- (id)initWithBar:(NSInteger)bar;+ (instancetype)fooWithBar:(NSInteger)bar;

但如果你用instancetype,代码如下:

- (instancetype)initWithBar:(NSInteger)bar;+ (instancetype)fooWithBar:(NSInteger)bar;

代码更加一致,可读性更强。它们返回相同的东西,这一点一目了然。

结论

除非你有意为旧编译器写代码,不然你在合适的时候都应该用instancetype。

在写一条返回id的消息前,问自己:这个类返回实例吗?如果返回,用instancetype。

肯定有需要返回id的时候,但你用instancetype的频率应该会更高

注意:

instancetype只可以作为返回值类型,不可以像id那样作为参数类型



参考:http://clang.llvm.org/docs/LanguageExtensions.html#objective-c-features

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 水饺都冻一块了怎么办 wps卸载了后文件打不开怎么办 六角螺母拧滑了怎么办 梅花内六角螺丝扭滑丝了怎么办 眼镜螺丝滑丝了怎么办 大螺丝拆不下来怎么办 一字螺丝扭不动怎么办 带帽的螺丝拧花怎么办 螺丝拧不出来了怎么办 小六角螺丝滑丝怎么办 螺丝拧不下来了怎么办 固定水龙头的螺母扭不动怎么办 小螺丝帽拧花了怎么办 6角螺丝帽拧圆了怎么办 眼镜螺丝滑扣了怎么办 眼镜的螺丝掉了怎么办 螺丝松了怎么办小窍门 螺丝松了怎么办 小窍门 小螺丝完全滑牙怎么办 车座椅螺丝滑丝怎么办 塑料柱突然滑牙怎么办 螺丝底座滑丝了怎么办 螺丝拧木头不紧怎么办 六方螺母滑了怎么办 螺母滑丝怎么拧下来怎么办 安装车牌螺丝眼滑丝了怎么办 注塑机孔螺丝洞滑丝了怎么办 空调内机螺丝洞滑丝了怎么办 小螺丝钉滑牙了怎么办? 螺丝滑扣了怎么办 松 苹果8p螺丝滑了怎么办 座椅孔螺丝滑了怎么办 车轮胎螺丝突扣了怎么办 孔里面滑丝了怎么办 六口螺丝滑扣了怎么办 手机螺丝滑扣了怎么办 超极本没有网线接口怎么办 机动车禁令标识位置放置错误怎么办 六角头冲得不标准怎么办 滚丝杆烫手怎么办怎样能不烫手 带滚花的杆子里面脏怎么办