iOS之《Effective Objective-C 2.0》读书笔记(16)
来源:互联网 发布:淘宝格子铺是什么 编辑:程序博客网 时间:2024/05/23 19:42
第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
阅读全文
0 0
- iOS之《Effective Objective-C 2.0》读书笔记(16)
- <iOS读书笔记>之Effective Objective-C 2.0
- iOS之《Effective Objective-C 2.0》读书笔记(1)
- iOS之《Effective Objective-C 2.0》读书笔记(2)
- iOS之《Effective Objective-C 2.0》读书笔记(3)
- iOS之《Effective Objective-C 2.0》读书笔记(4)
- iOS之《Effective Objective-C 2.0》读书笔记(5)
- iOS之《Effective Objective-C 2.0》读书笔记(6)
- iOS之《Effective Objective-C 2.0》读书笔记(7)
- iOS之《Effective Objective-C 2.0》读书笔记(8)
- iOS之《Effective Objective-C 2.0》读书笔记(9)
- iOS之《Effective Objective-C 2.0》读书笔记(10)
- iOS之《Effective Objective-C 2.0》读书笔记(11)
- iOS之《Effective Objective-C 2.0》读书笔记(12)
- iOS之《Effective Objective-C 2.0》读书笔记(13)
- iOS之《Effective Objective-C 2.0》读书笔记(14)
- iOS之《Effective Objective-C 2.0》读书笔记(17)
- iOS之《Effective Objective-C 2.0》读书笔记(21)
- 微信资源
- Linux与JVM的内存关系分析
- Lua学习笔记
- java中的多维数组
- DirectX9学习(二)
- iOS之《Effective Objective-C 2.0》读书笔记(16)
- C-C++ static、extern
- [面试题] 不用算术运算符实现两个数的加法
- java中二维数组(多维数组)的理解认识
- 新路程------git 配置vim作为提交编辑器
- 扫码支付接支付宝生活号(原服务窗)
- 微信公众号开发一(接入配置)
- linux解压war包的命令
- sublimetext3 快捷键