今天研究代码的时候,发现居然返回 instancetype 类型,很惊讶自己学识浅显。
Clang的文档里提到instancetype is a contextual keyword that is only permitted in the result type of an Objective-C method.
也就是说,instancetype只能作为返回值,不能像id那样作为参数。
原来这种技术基本从iOS 5的UINavigationController里就开始应用了。
当一个类返回相同类的实例的时候使用 instancetype 是合适。
首先:做如下定义
<span style="line-height: 23.333332061767578px; font-family: inherit; color: rgb(188, 63, 170);">@interface Foo</span>:<span style="line-height: 23.333332061767578px; font-family: inherit; color: rgb(116, 119, 11);">NSObject</span> - (id)initWithBar:(NSInteger)bar; <span style="line-height: 23.333332061767578px; font-family: inherit; color: rgb(9, 141, 16);">// initializer </span>+ (id)fooWithBar:(NSInteger)bar; <span style="line-height: 23.333332061767578px; font-family: inherit; color: rgb(188, 63, 170);"> // convenience constructor @end</span>
编译器不会自动将 id 转换为 instancetype 。
对于 init,他变得更加的复杂。比如当你写成如下格式
- (id)initWithBar:(NSInteger)bar
编译器会用如下形式保护起来:
- (instancetype)initWithBar:(NSInteger)bar这对使用ARC编译是很有必要的,同样因为编译器会做如上保护,有些人会告诉你没有必要使用 instancetype.
但是使用instancetype 有以下有利点。
一。更加明确:
在技术层面上讲,使用 intancetype 代替 id 是没有什么有利点儿。但是不能因为 init 编译器会将 id 转换成 instancetype,你就以此为借口。确实下面两个方法是相同的。
- (id)initWithBar:(NSInteger)bar; - (instancetype)initWithBar:(NSInteger)bar;但是至少在你眼里看出来是不同的。
二。模式化(Pattern) (不知道怎么翻译)
虽然 id 和 instancetype 对于 init 是一样的,但是对于构造函数来说他们是不一样的。
以下是不等价的:
+ (id)fooWithBar:(NSInteger)bar; + (instancetype)fooWithBar:(NSInteger)bar;使用第二种返回构造着时候,你每次都会得到正确的结果。
三。一致性
当你使用 init 和 构造函数 (convenience constructor)时候,两者混合在一起
- (id)initWithBar:(NSInteger)bar; + (instancetype)fooWithBar:(NSInteger)bar;
当你一致时候:
- (instancetype)initWithBar:(NSInteger)bar; + (instancetype)fooWithBar:(NSInteger)bar;
使用 instacetype ,能够让其他方法有诸如 +alloc +new -autorelease -init -retain 一样的特殊特性。
如下方法:
<code style="line-height: 28px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: gray;">#import "AppDelegate.h"</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@interface</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Foo</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">:</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">NSObject</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@end</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@implementation</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Foo</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">+</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">id</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">)</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">buildInstance </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">{</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 139);">return</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">self alloc</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> init</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">}</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">id</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">)</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">init </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">{</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 139);">return</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">super init</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">}</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@end</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@interface</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Bar</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">:</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Foo</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@end</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@implementation</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Bar</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 139);">void</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">)</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">doSometingElse </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">{</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">…</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">}</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@end</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@implementation</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">AppDelegate</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">-</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 139);">void</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">)</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">applicationDidFinishLaunching</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">:(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">NSNotification</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">*)</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">aNotification </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">{</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Foo</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> buildInstance</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> doSometingElse</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Bar</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> buildInstance</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> doSometingElse</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Foo</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> alloc</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> init</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> doSometingElse</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Bar</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> alloc</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> init</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> doSometingElse</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">}</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">@end</span></code>尽管下面两行
<code style="line-height: 28px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Foo</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> buildInstance</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> doSometingElse</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Foo</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> alloc</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> init</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> doSometingElse</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span></code>
对于编译器来说是一样的(buildInstance 和 init 都返回 id),我们仅仅在 init 方法得到一个错误。
但是在
<code style="line-height: 28px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Bar</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> alloc</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> init</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> doSometingElse</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]<span style="white-space: pre;"> </span></span></code>
没有错误!
[[Foo alloc] init] 正确的返回了 Foo 类型,[[Bar alloc] init] 正确的返回了 Bar 类型,但是我们没有看到 [[Foo buildInstance] doSometingElse]
or [[Bar buildInstance] doSometingElse].
如果使用 intancetype 代替 id,如:
<code style="line-height: 28px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">+</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">(</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">instancetype</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">)</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">buildInstance </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">{</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(0, 0, 139);">return</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">self alloc</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> init</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">}</span></code>我们将会得到
翻译的很烂,我也就是将一些别人的东西总结起来。
原文:http://www.iwangke.me/2013/01/06/instancetype-vs-id-for-objective-c/
http://tewha.net/2013/02/why-you-should-use-instancetype-instead-of-id/
http://stackoverflow.com/questions/8972221/would-it-be-beneficial-to-begin-using-instancetype-instead-of-id