1. 有一个相同两个不同。相同
  2. Written by Mattt Thompson on Dec 10th,
  3.  
  4. Objective-C is a rapidly evolving language, in a way that you just don't see in established programming languages. ARC, object literals, subscripting, blocks: in the span of just three years, so much of how we program in Objective-C has been changed (for the better).
  5.  
  6. All of this innovation is a result of Apple's philosophy of vertical integration. Just as Apple's investment in designing its own chipsets gave them leverage to compete aggressively with their mobile hardware, so too has their investment in LLVM allowed their software to keep pace.
  7.  
  8. Clang developments range from the mundane to paradigm-changing, but telling the difference takes practice. Because we're talking about low-level language features, it's difficult to understand what implications they may have higher up with API design.
  9.  
  10. One such example is instancetype, the subject of this week's article.
  11.  
  12. In Objective-C, conventions aren't just a matter of coding best-practices, they are implicit instructions to the compiler.
  13.  
  14. For example, alloc and init both have return types of id, yet in Xcode, the compiler makes all of the correct type checks. How is this possible?
  15.  
  16. In Cocoa, there is a convention that methods with names like alloc, or init always return objects that are an instance of the receiver class. These methods are said to have a related result type.
  17.  
  18. Class constructor methods, although they similarly return id, don't get the same type-checking benefit, because they don't follow that naming convention.
  19.  
  20. You can try this out for yourself:
  21.  
  22. [[[NSArray alloc] init] mediaPlaybackAllowsAirPlay]; // ❗ "No visible @interface for `NSArray` declares the selector `mediaPlaybackAllowsAirPlay`"
  23.  
  24. [[NSArray array] mediaPlaybackAllowsAirPlay]; // (No error)
  25.  
  26. Because alloc and init follow the naming convention for being a related result type, the correct type check against NSArray is performed. However, the equivalent class constructor array does not follow that convention, and is interpreted as id.
  27.  
  28. id is useful for opting-out of type safety, but losing it when you do want it sucks.
  29.  
  30. The alternative, of explicitly declaring the return type ((NSArray *) in the previous example) is a slight improvement, but is annoying to write, and doesn't play nicely with subclasses.
  31.  
  32. This is where the compiler steps in to resolve this timeless edge case to the Objective-C type system:
  33.  
  34. instancetype is a contextual keyword that can be used as a result type to signal that a method returns a related result type. For example:
  35.  
  36. @interface Person
  37. + (instancetype)personWithName:(NSString *)name;
  38. @end
  39.  
  40. instancetype, unlike id, can only be used as the result type in a method declaration.
  41.  
  42. With instancetype, the compiler will correctly infer that the result of +personWithName: is an instance of a Person.
  43.  
  44. Look for class constructors in Foundation to start using instancetype in the near future. New APIs, such as UICollectionViewLayoutAttributes are using instancetype already.
  45.  
  46. 下面的翻译:原文地址:http://blog.csdn.net/wzzvictory/article/details/16994913
  47.  
  48. 一、什么是instancetype
  49.  
  50. instancetypeclang .5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象。我们都知道未知类型的的对象可以用id关键字表示,那为什么还会再有一个instancetype呢?
  51. 二、关联返回类型(related result types
  52.  
  53. 根据Cocoa的命名规则,满足下述规则的方法:
  54.  
  55. 、类方法中,以allocnew开头
  56.  
  57. 、实例方法中,以autoreleaseinitretainself开头
  58.  
  59. 会返回一个方法所在类类型的对象,这些方法就被称为是关联返回类型的方法。换句话说,这些方法的返回结果以方法所在的类为类型,说的有点绕口,请看下面的例子:
  60. [objc] view plaincopyCODE上查看代码片派生到我的代码片
  61.  
  62. @interface NSObject
  63. + (id)alloc;
  64. - (id)init;
  65. @end
  66.  
  67. @interface NSArray : NSObject
  68. @end
  69.  
  70. 当我们使用如下方式初始化NSArray时:
  71. [objc] view plaincopyCODE上查看代码片派生到我的代码片
  72.  
  73. NSArray *array = [[NSArray alloc] init];
  74.  
  75. 按照Cocoa的命名规则,语句[NSArray alloc] 的类型就是NSArray*因为alloc的返回类型属于关联返回类型。同样,[[NSArray alloc]init] 的返回结果也是NSArray*。
  76.  
  77. 三、instancetype作用
  78. 、作用
  79.  
  80. 如果一个不是关联返回类型的方法,如下:
  81. [objc] view plaincopyCODE上查看代码片派生到我的代码片
  82.  
  83. @interface NSArray
  84. + (id)constructAnArray;
  85. @end
  86.  
  87. 当我们使用如下方式初始化NSArray时:
  88. [objc] view plaincopyCODE上查看代码片派生到我的代码片
  89.  
  90. [NSArray constructAnArray];
  91.  
  92. 根据Cocoa的方法命名规范,得到的返回类型就和方法声明的返回类型一样,是id
  93.  
  94. 但是如果使用instancetype作为返回类型,如下:
  95. [objc] view plaincopyCODE上查看代码片派生到我的代码片
  96.  
  97. @interface NSArray
  98. + (instancetype)constructAnArray;
  99. @end
  100.  
  101. 当使用相同方式初始化NSArray时:
  102. [objc] view plaincopyCODE上查看代码片派生到我的代码片
  103.  
  104. [NSArray constructAnArray];
  105.  
  106. 得到的返回类型和方法所在类的类型相同,是NSArray*!
  107.  
  108. 总结一下,instancetype的作用,就是使那些非关联返回类型的方法返回所在类的类型!
  109. 、好处
  110.  
  111. 能够确定对象的类型,能够帮助编译器更好的为我们定位代码书写问题,比如:
  112. [objc] view plaincopyCODE上查看代码片派生到我的代码片
  113.  
  114. [[[NSArray alloc] init] mediaPlaybackAllowsAirPlay]; // "No visible @interface for `NSArray` declares the selector `mediaPlaybackAllowsAirPlay`"
  115.  
  116. [[NSArray array] mediaPlaybackAllowsAirPlay]; // (No error)
  117.  
  118. 上例中第一行代码,由于[[NSArray alloc]init]的结果是NSArray*,这样编译器就能够根据返回的数据类型检测出NSArray是否实现mediaPlaybackAllowsAirPlay方法。有利于开发者在编译阶段发现错误。
  119.  
  120. 第二行代码,由于array不属于关联返回类型方法,[NSArray array]返回的是id类型,编译器不知道id类型的对象是否实现了mediaPlaybackAllowsAirPlay方法,也就不能够替开发者及时发现错误。
  121. 四、instancetypeid的异同
  122. 、相同点
  123.  
  124. 都可以作为方法的返回类型
  125. 、不同点
  126.  
  127. instancetype可以返回和方法所在类相同类型的对象,id只能返回未知类型的对象;
  128.  
  129. instancetype只能作为返回值,不能像id那样作为参数,比如下面的写法:
  130. [objc] view plaincopyCODE上查看代码片派生到我的代码片
  131.  
  132. //err,expected a type
  133. - (void)setValue:(instancetype)value
  134. {
  135. //do something
  136. }
  137.  
  138. 就是错的,应该写成:
  139. [objc] view plaincopyCODE上查看代码片派生到我的代码片
  140.  
  141. - (void)setValue:(id)value
  142. {
  143. //do something
  144. }

(转)Objective-C中的instancetype和id区别的更多相关文章

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

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

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

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

  3. Objective-C中的instancetype和id关键字(转)

    转自:Objective-C中的instancetype和id关键字 一.什么是instancetype 同id一样,都是表示未知类型的的对象. 二.关联返回类型(related result typ ...

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

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

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

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

  6. Objective-C中的instancetype和id…

    作者:韩俊强 原文地址:http://control.blog.sina.com.cn/admin/article/article_add.php 转载请注明出处 一.什么是instancetype ...

  7. CSS中的class与id区别及用法

    转自http://www.divcss5.com/rumen/r3.shtml及http://www.jb51.net/css/35927.html 我们平常在用DIV CSS制作Xhtml网页页面时 ...

  8. Objective C中nil/Nil/NULL的区别

    nil:指向oc中对象的空指针 Nil:指向oc中类的空指针 NULL:指向其他类型的空指针,如一个c类型的内存指针 NSNull:在集合对象中,表示空值的对象 若obj为nil:[obj messa ...

  9. ios instancetype与id区别

    我们都知道未知类型的的对象可以用id关键字表示,那为什么还会再有一个instancetype呢? instancetype能返回相关联的类型(使那些非关联返回类型的方法返回所在类的类型):而id 返回 ...

随机推荐

  1. Codeforces13C–Sequence(区间DP)

    题目大意 给定一个含有N个数的序列,要求你对一些数减掉或者加上某个值,使得序列变为非递减的,问你加减的值的总和最少是多少? 题解 一个很显然的结果就是,变化后的每一个值肯定是等于原来序列的某个值,因为 ...

  2. 数据流模型、Storm数据流模型

  3. HTML的id,name,class

    HTML中的id是给JavaScript用的(document.getElementById()) HTML中的name是给JavaScript用的(formUploadFile.submit()) ...

  4. CSS入门基础

    认识CSS 传统HTML设计网页版面的缺点 CSS的特点 CSS的排版样式 13.1 认识CSS CSS的英文全名是Cascading Style Sheets,中文可翻译为串接式排版样式,并且CSS ...

  5. 非常实用的Android Studio快捷键

    One—打印Log 生成tag: logt 打印log: logm logd loge Two—代码提示 Ctrl + Alt + Space Three—代码移动 选中代码: Ctrl + w 向 ...

  6. android96 内存创建图片副本,画画板

    package com.itheima.copy; import android.os.Bundle; import android.app.Activity; import android.grap ...

  7. RHCA学习笔记:RH442-Unit5 队列原理

    NIT 5 Queuing Theory 队列原理 目标: 1.明白性能调优的关键术语       2. 应用队列技术解决性能问题       3.明白性能调优的复杂性   5.1    Introd ...

  8. Python之美[从菜鸟到高手]--Python垃圾回收机制及gc模块详解

    http://blog.csdn.net/yueguanghaidao/article/details/11274737

  9. 真相:中国版BBB用USB连电脑没有盘符的根本原因分析

    很多网友在问:为什么中国版的装完驱动插上板子没有显示端口号和69M的盘符??楼主发现,在开机启动的时候,加载g_multi模块时出现错误提示 invalid argument.         Emb ...

  10. Big Clock

    Problem Description Our vicar raised money to have the church clock repaired for several weeks. The ...