The id type simply says a method will return a reference to an object. It could be any object of any type.

The instancetype type says a method will return a reference to an object of the same type as the class on which this method was called.

instancetype is a newer feature of Objective-C that basically adds type safety. In many cases, you will see no difference in an app (as you have mentioned). But there are times when instancetype is useful to avoid having to type cast when you call the method.

For example, imagine a class called SomeClass with a method like this:

+(id)createSomeClass
{
return [[SomeClass alloc] init];
}

  

Then imagine a subclass of SomeClass called SomeSubclass, that overrides that method like this:

+(id)createSomeClass
{
return [[SomeSubclass alloc] init];
}

  

Now, if you want to create a SomeSubclass, you would have to do this:

SomeSubclass *obj = (SomeSubclass*)[SomeSubclass createSomeClass];

However, if createSomeClass returned instancetype instead of id, then you could write this instead:

SomeSubclass *obj = [SomeSubclass createSomeClass];

You will see many people switching to instancetype for return values from any initializer or class creator methods (like the ones shown above). In fact, many of Apple's own APIs have been changed to use instancetype instead of id.

From: http://raywenderlich.com/forums/viewtopic.php?f=38&t=8959  , thanks !

iOS instancetype or id ?的更多相关文章

  1. ios instancetype 和 id 的异同

    1.0 相同点:都可以作为方法的返回类型 2.0 不同点: a.instancetype 可以返回和方法所在类相同类型的对象   id 只能返回未知类型的对象 b. instancetype 只能作为 ...

  2. ios instancetype与id区别

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

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

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

  4. instancetype 与 id for Objective-C

    instancetype.id.NSObject的区别 - simalone   1.instancetype只能用于方法的返回类型,而id用处和NSObject *类似. 2.instancetyp ...

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

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

  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. iOS - instancetype

    OC是一门正在迅速发展的语言,ARC,object literals ,subscripting ,blocks,Auto Synthesis,让我们看到它惊人的改变.instancetype是cla ...

随机推荐

  1. iOS注册collcetionViewFlowLayout

    self.arr = [[NSMutableArray alloc] init]; for (int i = 0; i < 9; i++) { [self.arr addObject:[UIIm ...

  2. [Android Pro] StarUML 版本破解

    参考:http://bbs.chinapyg.com/thread-79022-1-1.html 官网下载地址 : http://staruml.en.softonic.com 各平台版本均适用,本文 ...

  3. ssh配置免密码登录

    日常工作中很多情况下都需要登录服务器进行管理,一般都是用ssh进行连接,为了防止密码外泄,可以配置下ssh的免密码登录. 首先服务器两台: A:43.224.34.* B:104.238.161.* ...

  4. oracle 监控

    sqlplus "/as sysdba" .监控当前数据库谁在运行什么SQL语句 SELECT osuser, username, sql_text from v$session ...

  5. c语言运算符

     一.op=形式的赋值操作符 int a=0; a+=1; //等价于 a=a+1;// a*=1;  二.op=类表达式 int a=10,b=5; a/=b;//等价于a=a/b; a*=b+1; ...

  6. IT人学习方法论(一):学习方向

    07年的时候曾经讲过一节Webcast,名叫<使您成为Windows专家的一些学习习惯 >.直到最近,还经常收到听众关于这一节课反馈和心得的电子邮件,可见学习方法论是大家非常关心的问题.因 ...

  7. const和#define 区别

    1: 编译器处理不同     define宏是在预处理阶段展开,const常量是编译运行阶段使用. 2:类型和安全检查不同 const常量有数据类型,而宏常量没有数据类型,仅仅是展开.编译器可以对前者 ...

  8. iOS源码之OC相册,可以循环查看图片

    #import "ViewController.h" #import "YZUIScrollView.h" #define kuan ([UIScreen ma ...

  9. smarty汇总

    Smarty:模板技术 实现功能:前后分离. 原理:主要通过Smarty核心类实现,调用display方法,将模板文件读取,用正则进行替换,替换完保存到临时文 件,将临时文件加载到当前页面. 配置文件 ...

  10. sqlserver 用户、账号、安全等问题小汇

    一.孤立账号 SQL Server 的用户安全管理分两层,整个SQL Server 服务器一层,每个数据库一层. 在服务器层的帐号,叫登录账户(SQL Server:服务器角色),可以设置它管理整个S ...