原贴地址:http://blog.csdn.net/lyy_whg/article/details/12846055

http://www.iwangke.me/2013/01/06/instancetype-vs-id-for-objective-c/

新的LLVM编译器为我们带来了ARC, Object Literal and Scripting, Auto Synthesis等特性,同时也引入了instancetype关键字。instancetype用来表示Related Result Types(相关返回类型),那么它与id有什么不同呢?

根据Cocoa的命名惯例,init, alloc这类的方法,如果以id作为返回类型,会返回类本身的类型。

1
2
3
@interface Person
- (id)initWithName:(NSString *)name;
+ (id)personWithName:(NSString *)name;

但类方法的返回类型,LLVM(或者说Clang)却无法判断,我们来看一段代码:

1
2
3
// You may get two warnings if you're using MRC rather than ARC
[[[NSArray alloc] init] mediaPlaybackAllowsAirPlay]; // ❗ "No visible @interface for `NSArray` declares the selector `mediaPlaybackAllowsAirPlay`"
[[NSArray array] mediaPlaybackAllowsAirPlay]; // It's OK. But You'll get a runtime error instead of a compile time one

[NSArray array]除非显式转换为(NSArray *),否则编译器不会有错误提示。如果使用instancetype就不会有这样的问题:

1
2
3
@interface Person
- (instancetype)initWithName:(NSString *)name;
+ (instancetype)personWithName:(NSString *)name;

简单来说,instancetype关键字,保证了编译器能够正确推断方法返回值的类型。这种技术基本从iOS 5的UINavigationController里就开始应用了。

Clang的文档里提到instancetype is a contextual keyword that is only permitted in the result type of an Objective-C method. 也就是说,instancetype只能作为返回值,不能像id那样作为参数。

最后留个问题:Objective-C 3.0的时候,会不会出现泛型呢?

Reference:

[oc] instancetype vs id for Objective-C 【转】的更多相关文章

  1. OC 类方法,对象方法,构造方法以及instancetype和id的异同

    OC 类方法,对象方法,构造方法以及instancetype和id的异同 类方法: 类方法是可以直接使用类的引用,不需要实例化就可以直接使用的方法.一般写一些工具方法. 类方法: 声明和实现的时候,以 ...

  2. 42.OC中instancetype与id的区别

    区别: 在ARC(Auto Reference Count)环境下: instancetype用来在编译期确定实例的类型,而使用id的话,编译器不检查类型,运行时检查类型 在MRC(Manual Re ...

  3. OC中instancetype与id的区别

    1.在ARC环境下: instancetype用来在编译期确定实例的类型,而使用id的话,编译器不检查类型, 运行时检查类型. 2.在MRC环境下: instancetype和id一样,不做具体类型检 ...

  4. OC基础--构造方法 id类型

    new方法实现原理: new做了三件事情 1.开辟存储空间  + alloc 方法 2.初始化所有的属性(成员变量) - init 方法 3.返回对象的地址 [Person new]; == [[Pe ...

  5. iOS 用instancetype代替id作返回类型有什么好处?

    2014-07-07更新:苹果在iOS 8中全面使用instancetype代替id Steven Fisher:只要一个类返回自身的实例,用instancetype就有好处. @interface ...

  6. 转载:Objective-C中的 instancetype 和 id 关键字

    Objective-C中的instancetype和id关键字 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/16994 ...

  7. Objective-C中的instancetype和id区别

    目录(?)[-] 有一个相同两个不同相同 Written by Mattt Thompson on Dec 10th 2012 一什么是instancetype 二关联返回类型related resu ...

  8. (转)Objective-C中的instancetype和id区别

    有一个相同两个不同.相同 Written by Mattt Thompson on Dec 10th, Objective-C is a rapidly evolving language, in a ...

  9. 【转】Objective-C中的instancetype和id关键字

    原文:http://blog.csdn.net/wzzvictory/article/details/16994913 一.什么是instancetype instancetype是clang 3.5 ...

随机推荐

  1. Error with mysqld_safe

    出处:http://bugs.mysql.com/bug.php?id=18403 Description: - I downloaded the binary file “Standard 5.0. ...

  2. ASP.NET抓取网页内容

    原文:ASP.NET抓取网页内容 一.ASP.NET 使用HttpWebRequest抓取网页内容 这种方式抓取某些页面会失败 不过,有时候我们会发现,这个程序在抓取某些页面时,是获不到所需的内容的, ...

  3. Excel和notepad++加之更换

    1. 替换文本的内容Tab长度,粘合剂Excel有效的变革在列. 2. 替换空行.这里用到回车跟换行.回车\r使光标回到最左,换行\n使光标下移一行. 版权声明:本文博客原创文章,博客,未经同意,不得 ...

  4. Visual Studio 2013 的 Browser Link 功能

    Visual Studio 2013 的 Browser Link 功能 最近公司弄新项目需要用 MVC,就把 IDE 升级到了 Visual Studio 2013,在开发的时候发现有好多请求一个本 ...

  5. beanutils通过SimpleProperty使用get或set方法赋值

    public class Employee { private String  firstName;    private String lastName;    public Employee() ...

  6. ref引用类型,数组型参数,out输出参数

    ref和out的相同点和不同点 共同点:都是引用传递不同点:ref的参数在调用之前一定要赋值,在方法调用的过程中可以不要赋值.    out的参数在调用之前可以不赋值,在方法调用的过程中一定要赋值. ...

  7. 实现$.fn.extend 和$.extend函数

    Jquery揭秘系列:实现$.fn.extend 和$.extend函数   前面我们扩展了bind方法和ready函数,这次我要讲一下$.fn.extend 和$.extend函数. 其他的不多说, ...

  8. strlen与sizeof有什么区别?

    其实二者的区别还是很大的,首先strlen计算的长度不包括字符串最后的终止null字节, 而sizeof则计算包括终止null的缓冲区长度.还有一点很容易被忽略,strlen需要一次函数调用, 即它是 ...

  9. 关于HTTP头标

    对于HTTP中的头字段,我表示真的好麻烦,特找来一段资料共享.希望能对大家有用. HTTP的头域包括通用头,请求头,响应头和实体头四个部分.每个头域由一个域名,冒号(:)和域值三部分组成.域名是大小写 ...

  10. Intellij Idea/Webstorm/Phpstorm

    Intellij Idea/Webstorm/Phpstorm 的高效快捷键   1. shift + F6可以理解为F2的豪华重量版,不但可以重命名文件名,而且可以命名函数名,函数名可以搜索引用的文 ...