`

instancetype vs id

 
阅读更多

http://blog.csdn.net/numbbuaa/article/details/8556888

 

昨天刚发布了ios6.1版本,查看了一些苹果的官方api文档,看到一个instancetype关键字,几个UICollectionViewLayout的类方法,返回类型由id修改为instancetype,这两者有什么区别呢,上网搜了一下,

 

意思大概是,依照cocoa的命名规则,alloc,init这类方法,如果以id为返回类型,会返回类本身的类型,但类方法的返回类型,LLVM(clang)编译器无法判断,也就是说如果用id作为返回类型,有可能会将一个编译时错误变为一个运行时错误,instancetype则能够保证编译器正确判断返回的类型。

分享到:
评论

相关推荐

    instanceof和typeof运算符的区别详解

    两个运算符虽然比较相似,其实区别还是非常大的,虽然不难区别,但是对于初学者可能稍有困扰,下面就简单介绍一下它们两者的区别,希望对需要的朋友有所帮助

    LNInterpolation:可可和可可触摸的插值框架

    - ( instancetype )interpolateToValue:( id )toValue progress:( double )progress behavior:(LNInterpolationBehavior)behavior; Swift public func interpolate(to toValue: Any, progress: Double) -> Self ...

    分页效果设置

    @protocol ZSegmentedControlDelegate ...@property (nonatomic, weak)id <ZSegmentedControlDelegate> delegate; //@property (nonatomic, copy) void (^indexChangeBlock)(NSUInteger index);

    DMAppearance

    @end@implementation MyObject+ ( instancetype ) appearance{ return ( id )[DMAppearanceRecorder appearanceRecorderForClass: self ];}@end然后,当您的对象准备好接受外观样式时,您可以调用: [...

    ZYGCD计时器:GCD计时器

    ZYGCDTimer ZYGCDTimer主要用于替代NSTimer ,不会强持有目标,基于进行修改的,本质上是...+ ( instancetype )timerWithTimeInterval:( NSTimeInterval )interval target:( id )aTarget selector:( SEL )aSelector

    一个支持图文混排的ActionSheet

    - (instancetype)initWithTitle:(NSString *)title delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle otherButtonTitles:...

    CKComponentFadeTransition:用于添加淡入淡出过渡的 ComponentKit 扩展

    CKComponentFadeTransition ...+ ( instancetype )newWithURL:( NSURL *)url imageDownloader:( id )imageDownloader scenePath:( id )scenePath size:( const CKComponentSize &)size options:( const

    导航选项的菜单控件

    @property (weak, nonatomic) id delegate; @property (strong, nonatomic) UIColor *textColor; @property (strong, nonatomic) UIColor *separatarColor; - (instancetype)initWithItems:(NSArray *)items ...

    粒子云框架

    粒子云框架 粒子云框架是一种云资源供应框架,可以完全自定义和扩展,可以通过代码调用,并且不需要手动维护资源状态。 粒子云框架可实现对分层云基础架构进行建模,自动化部署以及管理... "InstanceType": "t2.micro",

    CRLoom:用于在核心数据中线程化NSManagedObject创建,更新和查询的框架

    概述NSManagedObjectImportOperation 这是通过创建的NSOperation子类+ (instanceType)operationWithData:( id )data managedObjectClass:( Class )class guaranteedInsert:( BOOL )guaranteedInsert saveOnBatchSize...

    ios-封装了FMDB,用户只需要关心字段和字段值,逻辑完全不要关心,用起来及其方便快捷,非常适合新手.zip

    (instancetype)intance; /** 数据库中是否存在表 */ - (BOOL)isExistWithTableName:(NSString*)name; /** 默认建立主键id 创建表(如果存在则不创建) , keys 数据存放要求@[字段名称1,字段名称2] */ ...

    ABCounterViewController:具有递增和递减功能的简约计数器控件

    应使用指定的初始化程序创建 ABCounterViewController 的实例: -(instancetype) initWithDelegate:(id<ABCounterViewController)delegate startCount:(float)startCount minCount:(float)minCount maxCount:...

    iOS中UIAlertView警告框组件的使用教程

    – (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /**/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles,...

    WSChartView:绘制折线图或条形图的框架

    介绍WSChartView WSLineChartView WSBarChartView WSData WSBar WSLabel用法在里面- (instancetype)initWithFrame:(CGRect)frame WithChartViewStyle:(WSChartViewStyle)Style WithDataSource:(id)source;...

    Android实现根据评分添加星级条

    简述 在仿写豆瓣的时候,发现了根据评分不同,星级数也不同的星级条。 百度一搜,发现Android有自带控件UIRatingBar,而iOS得要自己写…好吧,那就写吧。 图片素材 ...- (instancetype)initWithFram

Global site tag (gtag.js) - Google Analytics