iOS之《Effective Objective-C 2.0》读书笔记(16)
2017-08-24 11:43
387 查看
第16条:提供“全能初始化方法”
1.什么是“全能初始化方法”?
概念:提供必要信息的初始化方法,其它初始化方法的调用在本质上都是在调用它以NSDate为例介绍:initWithTimeIntervalSinceReferenceDate就是NSDate的全能初始化方法,其余的初始化方法都要调用它,只在此方法里储存数据和作出修改
- (instancetype)init - (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval) - (instancetype)initWithCoder:(NSCoder *)aDecoder - (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs; - (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date; //比如init方法的内部实现: -(instancetype)init{ return [self initWithTimeIntervalSinceReferenceDate:0]; }
2.子类也有自己的“全能初始化方法”,应该怎样维持调用?
(1)在子类中调用父类的全能初始化方法父类:YXRectangle
- (instancetype)init{ return [self initWithWidth:500 height:500]; } // 父类的全能初始化方法 - (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height { self = [super init]; if (self) { _width = width; _height = height; } return self; }
子类:YXSquare
- (instancetype)init{ return [self initWithDimension:500]; } // 子类的全能初始化方法 - (instancetype)initWithDimension:(CGFloat)dimension { // 调用父类的全能初始化方法 return [super initWithWidth:dimension height:dimension]; }
(2)在子类中阻止使用者直接调用父类的全能初始化方法
子类中
-(instancetype)init{ return [self initWithDimension:500]; } // 父类的全能初始化方法 - (instancetype)initWithWidth:(CGFloat)width height:(CGFloat)height{ @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Must be use initWithDimension :instead" userInfo:nil];// 只要使用该方法,NSException就会使程序崩溃 } // 子类的全能初始化方法 - (instancetype)initWithDimension:(CGFloat)dimension { return [super initWithWidth:dimension height:dimension]; }
3.如果一个类有两种全能初始化方法呢?
方法一:initWithFrame,此处会如上调用方法二:initWithCoder ,此时需要分为两步调用
-(id)initWithCoder:(NSCoder *)decoder{ // 1>调用超类 if (self == [super initWithCoder:decoder]){ // 2>实现本身操作 } return self; }
PS:简单整理学到的两个知识点
1.NSException是用来做什么的?控制程序的崩溃,经常看到的崩溃日志就是拜它所赐
具体使用
//定义 NSException *exception = [NSException exceptionWithName:name reason:reason userInfo:userInfo]; //抛异常 @throw exception;
2.对比学习:initWithFrame与initWithCoder
initWithFrame:当控件不是从xib、storyboard中创建时,而是代码创建 ,会调用这个方法 initWithCode:当控件是从xib、storyboard中创建时,就会调用这个方法,然后通过awakeFromNib来唤醒,有个调用顺序:initWithCode->awakeFromNib
相关文章推荐
- iOS-Effective Objective-C 2.0 读书笔记(一)
- iOS-Effective Objective-C 2.0 读书笔记(二)
- iOS之《Effective Objective-C 2.0》读书笔记(43)
- iOS之《Effective Objective-C 2.0》读书笔记(44)
- iOS之《Effective Objective-C 2.0》读书笔记(48)
- iOS-Effective Objective-C 2.0 读书笔记(三)
- iOS之《Effective Objective-C 2.0》读书笔记(3)
- iOS之《Effective Objective-C 2.0》读书笔记(4)
- <<Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法>>读书笔记之objc_msgSend
- iOS之《Effective Objective-C 2.0》读书笔记(50)
- iOS之《Effective Objective-C 2.0》读书笔记(49)
- iOS之《Effective Objective-C 2.0》读书笔记(5)
- iOS之《Effective Objective-C 2.0》读书笔记(9)
- 编写高质量的iOS代码--Effective Objective-C 2.0 读书笔记
- iOS之《Effective Objective-C 2.0》读书笔记(21)
- iOS之《Effective Objective-C 2.0》读书笔记(22)
- iOS之《Effective Objective-C 2.0》读书笔记(2)
- 《Effective Objective-C 2.0 编写高质量iOS与OS X代码的52个有效方法》--读书笔记
- iOS之《Effective Objective-C 2.0》读书笔记(45)
- iOS之《Effective Objective-C 2.0》读书笔记(24)