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. eclipse快捷键Alt + / 失效

    最近电脑上的Eclipse没有了自动提示功能,也不是全部不提示,大多数情况下按下"alt+/"键还会产生提示,但是当我在java项目中邪main方法和syso的时候,"a ...

  2. September 19th 2016 Week 39th Monday

    We come nearest to the great when we are great in humility. 我们最为谦逊的时候越接近伟大. When you are powerful en ...

  3. September 3rd 2016 Week 36th Saturday

    Calculation never made a hero. 举棋不定,难以称雄. We change. We have to. Or we spend the rest of our lives f ...

  4. LinkIssue: Error 'LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or cor

    参考:http://blog.csdn.net/junjiehe/article/details/16888197 使用VisualStudio 编译链接中可能出现如下错误: LINK : fatal ...

  5. Sightseeing(poj 3463)

    题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...

  6. UVa1593_Allgnment_Of_Code

    /** start: integer; // begins hear stop: integer; // ends here s: string; c: char; // temp **/ //测试数 ...

  7. limits.h头文件

    CHAR,SHRT,INT ,LLONG加_MAX后缀表示最大,加_MIN后缀表示最小,加U前缀表示无符号 UCHAR_MIN ,UCHAR_MAX sizeof()计算数所用的空间 #include ...

  8. Jpinyin笔记

  9. 按键使用方法(二)------verilog

    这里我们要验证一键两用的情况:点击与长击,单击与双击 代码: /********************************Copyright*************************** ...

  10. Git撤销提交和修改相关操作

    团队开发中经常遇到错误删除文件,错误提交等情况,那么使用Git该如何正确的进行撤销和恢复呢? 一.增补提交 git commit –C HEAD –a --amend -C表示复用指定提交的提交留言, ...