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. [Android Pro] Android studio jni中调用Log输出调试信息

    reference to : http://www.linuxidc.com/Linux/2014-02/96341.htm Android 开发中,java 可以方便的使用调试信息Log.i, Lo ...

  2. 无根树转有根树(dfs,tree)

    #include <bits/stdc++.h> #include <iostream> #include <queue> #include <stdio.h ...

  3. Rotate partitions in DB2 on z

    Rotating partitions   You can use the ALTER TABLE statement to rotate any logical partition to becom ...

  4. hdu 1257 最少拦截系统

    #include<time.h> #include <cstdio> #include <iostream> #include<algorithm> # ...

  5. 浅谈我的编程之路——感谢引领我的leader

    在开发的道路上,就始终无法避开版本控制,哪怕你是独自一人进行开发,版本控制也是有必要的,从最早开始使用CVS,到后来使用SVN,再到git,最后又回到了SVN,但是不知道为什么真的对SVN很无爱. 现 ...

  6. Loadrunner之HTTP接口测试脚本实例

    接口测试的原理是通过测试程序模拟客户端向服务器发送请求报文,服务器接收请求报文后对相应的报文做出处理然后再把应答报文发送给客户端,客户端接收应答报文结果与预期结果进行比对的过程,接口测试可以通过Jav ...

  7. **PHP中替换换行符

    PHP中替换换行符 php 不同系统的换行不同系统之间换行的实现是不一样的linux 与unix中用 \nMAC 用 \rwindow 为了体现与linux不同 则是 \r\n所以在不同平台上 实现方 ...

  8. SQL中的多表查询,以及JOIN的顺序重要么?

    说法是,一般来说,JOIN的顺序不重要,除非你要自己定制driving table. 示例: SELECT a.account_id, c.fed_id, e.fname, e.lname -> ...

  9. DevExpress DXperience 的本地化(汉化)方法

    Devexpress的.net组件目前非常流行,在国内开发者中有非常高的热度,但是由于是国外控件,我们经常遇到的一个问题是汉化.目前Devexpress公司2011.2版以后使用了统一的本地化模式,针 ...

  10. git branch用法总结

    git branch      git branch 不带参数:列出本地已经存在的分支,并且在当前分支的前面加“*”号标记,例如:   #git branch* master   newbranch ...