instancetype 对比 id 的好处
转自:http://blog.csdn.net/yangzychina/article/details/8818941
今天研究代码的时候,发现居然返回 instancetype 类型,很惊讶自己学识浅显。
Clang的文档里提到instancetype is a contextual keyword that is only permitted in the result type of an Objective-C method.
也就是说,instancetype只能作为返回值,不能像id那样作为参数。
原来这种技术基本从iOS 5的UINavigationController里就开始应用了。
当一个类返回相同类的实例的时候使用 instancetype 是合适。
首先:做如下定义
@interface Foo:NSObject
- (id)initWithBar:(NSInteger)bar; // initializer
+ (id)fooWithBar:(NSInteger)bar; // convenience constructor
@end
编译器不会自动将 id 转换为 instancetype 。
对于 init,他变得更加的复杂。比如当你写成如下格式
- (id)initWithBar:(NSInteger)bar
编译器会用如下形式保护起来:
- (instancetype)initWithBar:(NSInteger)bar
这对使用ARC编译是很有必要的,同样因为编译器会做如上保护,有些人会告诉你没有必要使用 instancetype.
但是使用instancetype 有以下有利点。
一。更加明确:
在技术层面上讲,使用 intancetype 代替 id 是没有什么有利点儿。但是不能因为 init 编译器会将 id 转换成 instancetype,你就以此为借口。确实下面两个方法是相同的。
- (id)initWithBar:(NSInteger)bar;
- (instancetype)initWithBar:(NSInteger)bar;
但是至少在你眼里看出来是不同的。
二。模式化(Pattern) (不知道怎么翻译)
虽然 id 和 instancetype 对于 init 是一样的,但是对于构造函数来说他们是不一样的。
以下是不等价的:
+ (id)fooWithBar:(NSInteger)bar;
+ (instancetype)fooWithBar:(NSInteger)bar;
使用第二种返回构造着时候,你每次都会得到正确的结果。
三。一致性
当你使用 init 和 构造函数 (convenience constructor)时候,两者混合在一起
- (id)initWithBar:(NSInteger)bar;
+ (instancetype)fooWithBar:(NSInteger)bar;
当你一致时候:
- (instancetype)initWithBar:(NSInteger)bar;
+ (instancetype)fooWithBar:(NSInteger)bar;
使用 instacetype ,能够让其他方法有诸如 +alloc +new -autorelease -init -retain 一样的特殊特性。
如下方法:
#import "AppDelegate.h"@interfaceFoo:NSObject@end@implementationFoo+(id)buildInstance {return[[self alloc] init];}-(id)init {return[super init];}@end@interfaceBar:Foo@end@implementationBar-(void)doSometingElse {…}@end@implementationAppDelegate-(void)applicationDidFinishLaunching:(NSNotification*)aNotification {[[Foo buildInstance] doSometingElse];[[Bar buildInstance] doSometingElse];[[[Foo alloc] init] doSometingElse];[[[Bar alloc] init] doSometingElse];}@end
尽管下面两行
[[Foo buildInstance] doSometingElse];[[[Foo alloc] init] doSometingElse];
对于编译器来说是一样的(buildInstance 和 init 都返回 id),我们仅仅在 init 方法得到一个错误。
但是在
[[[Bar alloc] init] doSometingElse]
没有错误!
[[Foo alloc] init] 正确的返回了 Foo 类型,[[Bar alloc] init] 正确的返回了 Bar 类型,但是我们没有看到 [[Foo buildInstance] doSometingElse]
or [[Bar buildInstance] doSometingElse].
如果使用 intancetype 代替 id,如:
+(instancetype)buildInstance {return[[self alloc] init];}
我们将会得到
翻译的很烂,我也就是将一些别人的东西总结起来。
原文:http://www.iwangke.me/2013/01/06/instancetype-vs-id-for-objective-c/
http://tewha.net/2013/02/why-you-should-use-instancetype-instead-of-id/
http://stackoverflow.com/questions/8972221/would-it-be-beneficial-to-begin-using-instancetype-instead-of-id
instancetype 对比 id 的好处的更多相关文章
- iOS 用instancetype代替id作返回类型有什么好处?
2014-07-07更新:苹果在iOS 8中全面使用instancetype代替id Steven Fisher:只要一个类返回自身的实例,用instancetype就有好处. @interface ...
- OC 类方法,对象方法,构造方法以及instancetype和id的异同
OC 类方法,对象方法,构造方法以及instancetype和id的异同 类方法: 类方法是可以直接使用类的引用,不需要实例化就可以直接使用的方法.一般写一些工具方法. 类方法: 声明和实现的时候,以 ...
- 转载:Objective-C中的 instancetype 和 id 关键字
Objective-C中的instancetype和id关键字 作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/16994 ...
- Objective-C中的instancetype和id区别
目录(?)[-] 有一个相同两个不同相同 Written by Mattt Thompson on Dec 10th 2012 一什么是instancetype 二关联返回类型related resu ...
- (转)Objective-C中的instancetype和id区别
有一个相同两个不同.相同 Written by Mattt Thompson on Dec 10th, Objective-C is a rapidly evolving language, in a ...
- 【转】Objective-C中的instancetype和id关键字
原文:http://blog.csdn.net/wzzvictory/article/details/16994913 一.什么是instancetype instancetype是clang 3.5 ...
- instancetype和id的区别
一.什么是instancetype instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以 ...
- Objective-C中的instancetype与id的区别
一.什么是instancetype instancetype是clang 3.5开始,clang提供的一个关键字,表示某个方法返回的未知类型的Objective-C对象.我们都知道未知类型的的对象可以 ...
- instancetype 和 id 的区别
原文:http://blog.csdn.net/sirodeng/article/details/50516478 一.什么是instancetype instancetype是clang 3.5开始 ...
随机推荐
- JavaScript基础入门教程(四)
说明 前面三篇博客介绍了js中基本的知识点,包括变量类型及其转换.表达式.运算符等小知识点,这篇博客主要讲的是对象.如果你学过java等语言,你也许在下文的阅读中发现在js中的对象与java中的对象存 ...
- iOS:quartz2D绘图(处理图像,绘制图像并添加水印)
绘制图像既可以重写drawRect:方法并在该方法中绘制,也可以不用重写该方法,它有封装好的函数获取自己的图像绘制上下文,即UIGraphicsBeginImageContext(CGSize siz ...
- iOS:手势的详解UIGestureReconizer
手势类:UIGestureReconizer 父类: 手势状态枚举 typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { UIGesture ...
- AQTime教程
1 简介 AQTime和MemProof都是AutomatedQA旗下的产品,AQTime比MemProof提供了更丰富强大的功能.该产品含有完整的性能和调试工具集,能够收集程序运行时关键的性能信息和 ...
- vmware虚拟机 C硬盘空间 无损扩容 新测
摘自: http://hi.baidu.com/y276827893/item/78a351f427726549932af214 其实上面一步的话, 虚拟机设置 里选择磁盘,实用工具里也有这个功能的. ...
- web页面实时更新页面的原理--WebSocket
原文:https://www.jianshu.com/p/8f956cd4d42b angular-cli启动的项目也可以自动刷新,底下应该也是应用的websocket的原理. ----------- ...
- LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...
- Office WPS如何让页与页之间不相互影响
在一个页面结束的位置点击插入-分页符,完了之后测试按回车下一页的内容有没有跟着往下跑,如果还是跟着往下跑的,再插入一次分页符,一般插入多次之后就不会跟着跑了,但是插的太多会有空白页面 测试按回车, ...
- 《Java程序猿面试笔试宝典》之 什么是AOP
AOP(Aspect-Oriented Programming.面向切面编程)是对面向对象开发的一种补充,它同意开发者在不改变原来模型的基础上动态地改动模型从而满足新的需求.比如.在不改变原来业务逻辑 ...
- js实现图片的等比例缩放
js实现图片的等比例缩放 CreateTime--2018年3月6日14:04:18 Author:Marydon 1.代码展示 /** * 图片按宽高比例进行自动缩放 * @param ImgO ...