一、KVC 即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性。而不是通过调用Setter、Getter方法访问。KVO 就是基于 KVC 实现的关键技术之一。
Student *student = [[Student alloc] init]; //通过KVC设置name的值 [student setValue:@"Jacedy" forKey:@"name"]; //等效于:student.name = @"Jacedy" NSString *m_name = [student valueForKey:@"name"]; NSLog(@"%@", m_name); Course *course = [[Course alloc] init]; [course setValue:@"音乐" forKey:@"CourseName"]; [student setValue:course forKey:@"course"]; //通过键值径获取CourseName的值(KVC按照键值路径取值时,如果对象不包含指定的键值,会自动进入对象内部,查找对象属性) NSString *courseName = [student valueForKeyPath:@"course.CourseName"]; NSLog(@"课程名称:%@", courseName); //通过键值径设置CourseName的值 [student setValue:@"实验课" forKeyPath:@"course.CourseName"]; courseName = [student valueForKeyPath:@"course.CourseName"]; NSLog(@"课程名称:%@", courseName); //通过KVC设置NSInteger的值(使用KVC间接修改对象属性时,系统会自动判断对象属性的类型,并完成转换) [student setValue:@"88" forKeyPath:@"point"]; NSString *m_point = [student valueForKey:@"point"]; NSLog(@"分数:%@", m_point); //通过KVC操作集合 Student *student1 = [[Student alloc] init]; Student *student2 = [[Student alloc] init]; Student *student3 = [[Student alloc] init]; [student1 setValue:@"65" forKey:@"point"]; [student2 setValue:@"77" forKey:@"point"]; [student3 setValue:@"99" forKey:@"point"]; NSArray *array = [NSArray arrayWithObjects:student1, student2, student3, nil]; [student setValue:array forKey:@"otherStudent"]; NSLog(@"其他学生的成绩:%@", [student valueForKeyPath:@"otherStudent.point"]); //KVC的简单运算 NSLog(@"共 %@ 个学生", [student valueForKeyPath:@"otherStudent.@count"]); NSLog(@"最高成绩:%@", [student valueForKeyPath:@"otherStudent.@max.point"]); NSLog(@"最低成绩:%@", [student valueForKeyPath:@"otherStudent.@min.point"]); NSLog(@"平均成绩:%@", [student valueForKeyPath:@"otherStudent.@avg.point"]); /* KVC需要注意的地方: 1)key的值必须正确,如果拼写错误,会出现异常; 2)当key的值没有定义时,valueForUndefinedKey:方法会被调用,如果你自己写了这个方法,key的值出错就会调用到这里来; 3)因为类key反复嵌套,所以有个keyPath的概念,keyPath就是用点.号来把一个一个key链接起来,这样就可以根据这个路径访问下去; 4)NSArray、NSSet等都支持KVC */
二、KVO 的是KeyValue Observe的缩写,中文是键值观察。这是一个典型的观察者模式,观察者在键值改变时会得到通知。iOS中有个Notification的机制,也可以获得通知,但这个机制需要有个Center,相比之下KVO更加简洁而直接。
使用步骤:
1.注册需要观察的对象的属性addObserver:forKeyPath:options:context:
2.实现observeValueForKeyPath:ofObject:change:context:方法,这个方法当观察的属性变化时会自动调用3.取消注册观察removeObserver:forKeyPath:context:
// JKChild.h#import@interface JKChild : NSObject@property(nonatomic, assign) NSInteger happyVal;@end
// JKChild.m#import "JKChild.h"@implementation JKChild- init { if (self = [super init]) { self.happyVal = 100; //定时器,1秒钟调用一次timerAction:函数 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; } return self;}- (void)timerAction:(NSTimer *)timer { //方式一: self.happyVal--; //方式二:// _happyVal--; //直接修改不会触发监听,还需通过KVC方式设置// [self setValue:[NSNumber numberWithInteger:_happyVal] forKey:@"happyVal"];}@end
// JKNurse.h#import@class JKChild;@interface JKNurse : NSObject@property(nonatomic, strong) JKChild *child;- (id)initWithChild:(JKChild *)child;@end
// JKNurse.m#import "JKNurse.h"#import "JKChild.h"@implementation JKNurse- (id)initWithChild:(JKChild *)child { if (self = [super init]) { self.child = [child retain]; //KVO 注册监听,监听JKChild类中happyVal的值变化 [self.child addObserver:self forKeyPath:@"happyVal" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"xxx"]; } return self;}// 监听响应- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context { NSLog(@"keyPath:%@, object:%@,change:%@, context:%@", object, keyPath, change, context);}- (void)dealloc { // 移除监听 [self.child removeObserver:self forKeyPath:@"happyVal"]; [self.child release]; [super dealloc];}@end
// main.m#import#import "JKChild.h"#import "JKNurse.h"int main(int argc, const char * argv[]) { @autoreleasepool { JKChild *child = [[JKChild alloc] init]; JKNurse *nurse = [[JKNurse alloc] initWithChild:child]; //加入了定时器,通过runloop使事件持续运行 [[NSRunLoop currentRunLoop] run]; } return 0;}
三、Notification(通知)
// Child.h#import#define WEEK_INFOMATION @"WEEK"@interface Child : NSObject@property (nonatomic, assign) NSInteger sleep;@end
// Child.m#import "Child.h"@implementation Child- (instancetype) init{ self = [super init]; if (self != nil) { _sleep = 100; // 添加定时器 [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES]; } return self;}- (void)timerAction:(NSTimer *)timer{ _sleep -= 4; NSLog(@"%ld", (long)_sleep); if (_sleep < 90) { // 获取通知中心的单例后,给指定的名称发送通知 [[NSNotificationCenter defaultCenter] postNotificationName:WEEK_INFOMATION object:[NSNumber numberWithInteger:_sleep]]; // 停止定时器 [timer invalidate]; }}@end
// Father.h#import@interface Father : NSObject@end
// Father.m#import "Father.h"#import "Child.h"@implementation Father- (instancetype)init{ self = [super init]; if (self != nil) { // 监听接收通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(weekNotification:) name:WEEK_INFOMATION object:nil]; } return self;}- (void)weekNotification:(NSNotification *)notification{ NSLog(@"Father received object is : %@", notification.object); NSLog(@"week up!");}@end
Mother类代码与Father类代码相似,此处略过......
四、Delegate(代理)
// Boss.h#import#import "Sec.h"@interface Boss : NSObject //老板类@property(copy, nonatomic) NSString *name;@property(weak, nonatomic) id delegate;-(void)work;@end
// Boss.m#import "Boss.h"@implementation Boss@synthesize name;-(void)work{ NSLog(@"%@ 正在工作", name);}@end
// Sec.h#import#import "SecDelegate.h"@interface Sec : NSObject //秘书类@property(copy, nonatomic) NSString *name;@end
// Sec.m#import "Sec.h"@implementation Sec@synthesize name;-(void)phone{ NSLog(@"%@ 接到了电话", name);}@end
// SecDelegate.h#ifndef SecDelegate_h#define SecDelegate_h#import@protocol SecDelegate @optional // 默认为@required-(void)phone; // 接电话@end#endif /* SecDelegate_h */
// main.m// 代理模式#import#import "Boss.h"#import "Sec.h"int main(int argc, const char * argv[]) { @autoreleasepool { Boss *boss = [[Boss alloc] init]; [boss setName:@"刘老板"]; Sec *sec = [[Sec alloc] init]; [sec setName:@"张秘书"]; boss.delegate = sec; [boss work]; [sec phone]; } return 0;}
比较:
1)delegate 的 优势 :
1.非常严格的语法。所有将听到的事件必须是在delegate协议中有清晰的定义。
2.如果delegate中的一个方法没有实现那么就会出现编译警告/错误
3.协议必须在controller的作用域范围内定义
4.在一个应用中的控制流程是可跟踪的并且是可识别的;
5.在一个控制器中可以定义定义多个不同的协议,每个协议有不同的delegates
6.没有第三方对象要求保持/监视通信过程。
7.能够接收调用的协议方法的返回值。这意味着delegate能够提供反馈信息给controller
缺点 :
1.需要定义很多代码:1.协议定义;2.controller的delegate属性;3.在delegate本身中实现delegate方法定义
2.在释放代理对象时,需要小心的将delegate改为nil。一旦设定失败,那么调用释放对象的方法将会出现内存crash
3.在一个controller中有多个delegate对象,并且delegate是遵守同一个协议,但还是很难告诉多个对象同一个事件,不过有可能。
2)notification的 优势 :
1.不需要编写多少代码,实现比较简单;
2.对于一个发出的通知,多个对象能够做出反应,即1对多的方式实现简单
3.controller能够传递context对象(dictionary),context对象携带了关于发送通知的自定义的信息
缺点 :
1.在编译期不会检查通知是否能够被观察者正确的处理;
2.在释放注册的对象时,需要在通知中心取消注册;
3.在调试的时候应用的工作以及控制过程难跟踪;
4.需要第三方对喜爱那个来管理controller与观察者对象之间的联系;
5.controller和观察者需要提前知道通知名称、UserInfodictionary keys。如果这些没有在工作区间定义,那么会出现不同步的情况;
6.通知发出后,controller不能从观察者获得任何的反馈信息。
3)KVO的 优势 :
1.能够提供一种简单的方法实现两个对象间的同步。例如:model和view之间同步;
2.能够对非我们创建的对象,即内部对象的状态改变作出响应,而且不需要改变内部对象(SKD对象)的实现;
3.能够提供观察的属性的最新值以及先前值;
4.用key paths来观察属性,因此也可以观察嵌套对象;
5.完成了对观察对象的抽象,因为不需要额外的代码来允许观察值能够被观察
缺点 :
1.我们观察的属性必须使用strings来定义。因此在编译器不会出现警告以及检查;
2.对属性重构将导致我们的观察代码不再可用;
3.复杂的“IF”语句要求对象正在观察多个值。这是因为所有的观察代码通过一个方法来指向;
4.当释放观察者时不需要移除观察者。
4) 效率肯定是delegate比NSNotification高。
delegate方法比notification更加直接,最典型的特征是,delegate方法往往需要关注返回值,也就是delegate方法的结果。比如-windowShouldClose:,需要关心返回的是yes还是no。所以delegate方法往往包含 should这个很传神的词。也就是好比你做我的delegate,我会问你我想关闭窗口你愿意吗?你需要给我一个答案,我根据你的答案来决定如何做下一步。相反的,notification最大的特色就是不关心接受者的态度,我只管把通告放出来,你接受不接受就是你的事情,同时我也不关心结果。所以notification往往用did这个词汇,比如NSWindowDidResizeNotification,那么NSWindow对象放出这个notification后就什么都不管了也不会等待接 受者的反应。
5)KVO和NSNotification的区别:
和delegate一样,KVO和NSNotification的作用也是类与类之间的通信,与delegate不同的是:1)这两个都是负责发出通知,剩下的事情就不管了,所以没有返回值;2)delegate只是一对一,而这两个可以一对多。这两者也有各自的特点。
6)delegate针对one-to-one关系,并且reciever可以返回值给sender;notification 可以针对one-to-one/many/none,reciever无法返回值给sender;所以,delegate用于sender希望接受到reciever的某个功能反馈值,notification用于通知多个object某个事件。