原贴地址: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. jquery+html三级联动下拉框及详情页面加载时的select初始化问题

    html写的三个下拉框,如下: <select name="ddlQYWZYJ" id="ddl_QYWZYJ" class="fieldsel ...

  2. OpenSUSE 13.2安装Texlive2014+Texmaker+Lyx

    (1)首先下载安装Texlive2014 地址:http://mirrors.ustc.edu.cn/CTAN/systems/texlive/Images/ 或直接下载: wget http://m ...

  3. Asterisk 未来之路3.0_0006

    原文:Asterisk 未来之路3.0_0006 Modules Asterisk 是基于模块构建的.一个模块提供某个特定的功能,它是动态的被装载.比如:信道驱动(chan_sip.so),或可以连接 ...

  4. 用批处理编译*.sln工程

    原文:用批处理编译*.sln工程 批处理是直接调用Microsoft Visual Studio 8\Common7\IDE\ 目录内的 devenv.exe ,它启动后就是IDE,提供的参数如下: ...

  5. leetcode[71] Sqrt(x)

    题目,就是实现一个开方,返回是整数.int sqrt(int x) 用二分法,因为一个数的开方肯定小于 x/2 + 1, 因为小于5的某些数的开方并不一定比x/2小,所以要+1,那么们定义一个left ...

  6. PhpStorm创建Drupal模块项目开发教程

    在PhpStorm开发工具中,创建Drupal开发项目有两种方式:整合Drupal到现有的项目中和直接创建一个新的Drupal模块. 接下来将展示这两种方式的具体操作! 整合Drupal到现有的项目 ...

  7. 学习PHP时的一些总结(四)

    目录的基本操作: 在系统的每个目录下都有两个特殊的目录"."和".." , 分别指示当前目录和当前目录的父目录. dirname()  返回目录的名称 path ...

  8. 安装WindowsXP操作系统(Ghost版) - 初学者系列 - 学习者系列文章

    Windows XP的Ghost版是经典的版本.因为XP相对较小些,所以用Ghost起来速度比较快.如果Ghost那个Windows 7之类的,速度就慢了.Windows 7建议还是安装比较快.下面简 ...

  9. Asp.Net Identity 深度解析 之 注册登录的扩展

    关于权限每个系统都有自己的解决方案,今天我们来讨论一下微软的权限框架Asp.Net Identity ,介绍如下  http://www.asp.net/identity 这里不在赘余. 很多人认为 ...

  10. C#编程实践—EventBroker简单实现

    前言 话说EventBroker这玩意已经不是什么新鲜货了,记得第一次接触这玩意是在进第二家公司的时候,公司产品基础架构层中集成了分布式消息中间件,在.net基础服务层中使用EventBroker的模 ...