一、什么是instancetype

instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象。我们都知道未知类型的的对象可以用id关键字表示,那为什么还会再有一个instancetype呢?

二、关联返回类型(related result types)

根据Cocoa的命名规则,满足下述规则的方法:

1、类方法中,以alloc或new开头

2、实例方法中,以autorelease,init,retain或self开头

会返回一个方法所在类类型的对象,这些方法就被称为是关联返回类型的方法。换句话说,这些方法的返回结果以方法所在的类为类型,说的有点绕口,请看下面的例子:

  1. @interface NSObject
  2. + (id)alloc;
  3. - (id)init;
  4. @end
  5. @interface NSArray : NSObject
  6. @end
当我们使用如下方式初始化NSArray时:
  1. NSArray *array = [[NSArray alloc] init];
按照Cocoa的命名规则,语句[NSArray alloc] 的类型就是NSArray*因为alloc的返回类型属于关联返回类型。同样,[[NSArray alloc]init] 的返回结果也是NSArray*。

三、instancetype作用

1、作用

如果一个不是关联返回类型的方法,如下:

  1. @interface NSArray
  2. + (id)constructAnArray;
  3. @end

当我们使用如下方式初始化NSArray时:

  1. [NSArray constructAnArray];
根据Cocoa的方法命名规范,得到的返回类型就和方法声明的返回类型一样,是id。

但是如果使用instancetype作为返回类型,如下:

  1. @interface NSArray
  2. + (instancetype)constructAnArray;
  3. @end
当使用相同方式初始化NSArray时:
  1. [NSArray constructAnArray];
得到的返回类型和方法所在类的类型相同,是NSArray*!

总结一下,instancetype的作用,就是使那些非关联返回类型的方法返回所在类的类型!

2、好处

能够确定对象的类型,能够帮助编译器更好的为我们定位代码书写问题,比如:

  1. [[[NSArray alloc] init] mediaPlaybackAllowsAirPlay]; //  "No visible @interface for `NSArray` declares the selector `mediaPlaybackAllowsAirPlay`"
  2. [[NSArray array] mediaPlaybackAllowsAirPlay]; // (No error)
上例中第一行代码,由于[[NSArray alloc]init]的结果是NSArray*,这样编译器就能够根据返回的数据类型检测出NSArray是否实现mediaPlaybackAllowsAirPlay方法。有利于开发者在编译阶段发现错误。

第二行代码,由于array不属于关联返回类型方法,[NSArray array]返回的是id类型,编译器不知道id类型的对象是否实现了mediaPlaybackAllowsAirPlay方法,也就不能够替开发者及时发现错误。

四、instancetype和id的异同

1、相同点

都可以作为方法的返回类型

2、不同点

①instancetype可以返回和方法所在类相同类型的对象,id只能返回未知类型的对象;

②instancetype只能作为返回值,不能像id那样作为参数,比如下面的写法:

  1. //err,expected a type
  2. - (void)setValue:(instancetype)value
  3. {
  4. //do something
  5. }
就是错的,应该写成:
  1. - (void)setValue:(id)value
  2. {
  3. //do something
  4. }

instancetype和id的区别的更多相关文章

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

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

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

    要区分instancetype和id,首先要弄清楚什么是关联返回类型(Related Result Type). 关联返回类型即一个方法的返回类型就是调用这个方法的调用者的类型.具有下列条件的方法具有 ...

  3. Objective-C中的instancetype与id的区别

    一.什么是instancetype instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以 ...

  4. instancetype 和 id 的区别

    原文:http://blog.csdn.net/sirodeng/article/details/50516478 一.什么是instancetype instancetype是clang 3.5开始 ...

  5. 89、instancetype和id的区别

    1>instancetype在类型表示上,跟id一样,可以表示任何对象类型 2>instancetype只能用在返回值类型上,不能像id一样用在参数类型上 3>instancetyp ...

  6. OC中instancetype与id的区别

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

  7. instancetype和id的区别,objective-c

    instancetype   clang 3.5 提供的关键字,  表示:某方法返回未知类型的OC对象 都知道id任意类型关键字,为什么还会出现一个新的关键字? 返回关联类型 1.类方法中,alloc ...

  8. oc34--instancetype和id的区别

    // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int age; ...

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

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

随机推荐

  1. linux中查找命令find、locate、whereis、which、type区别

    linux中查找命令find.locate.whereis.which.type区别 1. find Java代码 find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件.与查询数据库(/ ...

  2. linux系统下svn服务器操作命令

    linux系统下svn服务器操作命令 .输出指定文件或URL的内容.  svncat 目标[@版本]…如果指定了版本,将从指定的版本开始查找. svncat -r PREV filename > ...

  3. Deploy a Sharded Cluster

    Start the Config Server Database Instances for example :  mongod --configsvr --dbpath <path> - ...

  4. java 实现排序

    package com.cjs.sort; /** * 此类用来提供针对整数的各种排序算法 * * @author S * @version 1.0 */ public class MySort { ...

  5. <转载>构造函数与拷贝构造函数

    原文地址http://www.cnblogs.com/waynelu/archive/2012/07/01/2572264.html 构造函数 构造函数.析构函数与赋值函数是每个类最基本的函数. 对于 ...

  6. android 缓存Bitmap - 开发文档翻译

    由于本人英文能力实在有限,不足之初敬请谅解 本博客只要没有注明“转”,那么均为原创,转贴请注明本博客链接链接 Loading a single bitmap into your user interf ...

  7. pyqt 正则表达式例子学习

    def rex01(self): username=QtCore.QRegExp('[a-zA-Z0-9_]{2,10}') self.names.setValidator(QtGui.QRegExp ...

  8. Working with Numbers in PL/SQL(在PL/SQL中使用数字)

    This article gives you all the information you need in order to begin working with numbers in your P ...

  9. eclipse在ubuntu13.04下崩溃crash

    错误信息: # # A fatal error has been detected by the Java Runtime Environment: # # SIGSEGV (0xb) at pc=0 ...

  10. easyUI的combobox设置隐藏和显示

    今天遇到一个需求,需要在combobox选择不同选项时,分别切换另一个控件为text或者combobox. 当时想了各种办法,想将combobx和text切换隐藏,但是都没得到自己想要的效果.最终还是 ...