原文链接:[原创]obj-c编程17:键值观察(KVO)
系列专栏链接:objective-c 编程系列
说完了前面一篇KVC,不能不说说它的应用KVO(Key-Value Observing)喽。KVO类似于ruby里的hook功能,就是当一个对象属性发生变化时,观察者可以跟踪变化,进而观察或是修正这个变化,这是通过回调观察者注册的回调函数来完成的。要使用键值观察,必须满足3个条件:
1 被观察对象必须对所观察属性使用符合KVC标准的存取器方法;
2 观察者必须实现接受通知的方法(回调方法):-observeValue:forKeyPath:ofObject:change:context:,该方法在值发生变化时被调用,并且可以定制行为:比如同时接收新值和原值以及其他自定义信息;
3 观察者通过-addObserver:forKeyPath:options:context:方法对特定对象的特定路径进行注册.
还要注意的是,在观察者完成对被观察者的观察后,必须将自己移除,否则当观察者释放后,来自被观察者的变化通知消息将无处可发,可能会导致引用崩溃。另外可以使用多个观察者对同一个对象的同一个路径进行观察啊,类似于形成一个观察链。
下面我将会写一个简单的例子,用代码说明上述内容:
1 #import <Foundation/Foundation.h> 2 3 #define msg(...) NSLog(__VA_ARGS__) 4 #define mki(x) [NSNumber numberWithInt:x] 5 6 @interface Son:NSObject{ 7 NSString *girl_friend_name; 8 } 9 @PRoperty NSString *girl_friend_name; 10 -(void)think; 11 -(void)fall_in_love_with:(NSString *)girl_name; 12 @end 13 14 @implementation Son 15 @synthesize girl_friend_name; 16 17 -(void)fall_in_love_with:(NSString *)girl_name{ 18 self.girl_friend_name = girl_name; 19 } 20 21 -(void)think{ 22 msg(@"My love girl is %@ ... Does baba know?",girl_friend_name); 23 } 24 @end 25 26 @interface Baba:NSObject{ 27 Son *son; 28 } 29 @property Son *son; 30 @end 31 32 @implementation Baba 33 @synthesize son; 34 35 -(id)initWithSon:(Son *)son_v{ 36 self = [super init]; 37 if(self){ 38 son = son_v; 39 [son addObserver:self forKeyPath:@"girl_friend_name" \ 40 options:(NSKeyValueObservingOptionNew|\ 41 NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial) \ 42 context:nil]; 43 } 44 return self; 45 } 46 47 -(void)observeValueForKeyPath:(NSString *)key_path \ 48 ofObject:(id)obj change:(NSDictionary *)change \ 49 context:(void *)context{ 50 51 NSString *old_name = [change objectForKey:NSKeyValueChangeOldKey]; 52 NSString *new_name = [change objectForKey:NSKeyValueChangeNewKey]; 53 54 if(!old_name){ 55 //NSKeyValueObservingOptionInitial选项的作用,第一次hook会发一次消息 56 //以后每次更改会发一次消息。 57 msg(@"Yes , It's first time to induction son's girl_name!!!"); 58 }else{ 59 if([new_name isEqualToString:@"libinbin"]){ 60 [obj fall_in_love_with:@"fanbinbin"]; 61 } 62 msg(@"My son's %@ change from %@ to %@ ... That's OK? :(",\ 63 key_path,old_name,new_name); 64 } 65 } 66 67 -(void)dealloc{ 68 [son removeObserver:self forKeyPath:@"girl_friend_name"]; 69 son = nil; 70 //[super dealloc]; 71 } 72 @end 73 74 int main(int argc,char *argv[]) 75 { 76 @autoreleasepool{ 77 Son *son = [[Son alloc] init]; 78 [son fall_in_love_with:@"lili"]; 79 [son think]; 80 81 Baba *baba = [[Baba alloc] initWithSon:son]; 82 [son fall_in_love_with:@"mimi"]; 83 [son think]; 84 85 //儿子本来喜欢的是libinbin,可是... 86 [son fall_in_love_with:@"libinbin"]; 87 //被他老爸用特异功能改成fanbinbin啦! 88 [son think]; 89 } 90 return 0; 91 }
简单说明下:老爸自然是要随时监控儿子的女朋友啊,可是这个老爸监视也就算了,还强行改变儿子的女朋友,这个就...随便想的一个例子,就当fun吧。说明下主要方法:
-addObserver注册对象的观察者,其中的options的其他可选值如下,可以多选:
其中的context参数意义不明哦,如果哪位知道的,别忘了告诉老猫我一下啊。NSKeyValueObservingOptionInitial标志的含义参见代码注释吧。
-observeValueForKeyPath回调方法中的change是一个字典参数,其中包括:
其中NSKeyValueChangeKindKey指定了接收到得变化类型,可能有以下几种值:
书上的代码在该方法定义的末尾部分增加了其对父类中同名方法的回溯调用:
[super observeValueForkeyPath:key_path ofObject:obj change:change context:ctx];
不过我在代码中没有写这个。书上在观察者的dealloc方法最后是有一句[super dealloc],但是这招在ARC下编译时会出错喽,暂时去掉吧。该段代码运行结果如下:
1 apple@kissAir: objc_src$./k 2 3 2014-07-06 20:11:26.757 k[2109:507] My love girl is lili ... Does baba know? 4 5 2014-07-06 20:11:26.759 k[2109:507] Yes , It's first time to induction son's girl_name!!! 6 7 2014-07-06 20:11:26.760 k[2109:507] My son's girl_friend_name change from lili to mimi ... That's OK? :( 8 9 2014-07-06 20:11:26.760 k[2109:507] My love girl is mimi ... Does baba know? 10 11 2014-07-06 20:11:26.761 k[2109:507] My son's girl_friend_name change from libinbin to fanbinbin ... That's OK? :( 12 13 2014-07-06 20:11:26.761 k[2109:507] My son's girl_friend_name change from mimi to libinbin ... That's OK? :( 14 15 2014-07-06 20:11:26.761 k[2109:507] My love girl is fanbinbin ... Does baba know?
爸爸监管的不错,可是儿子不高兴喽。hack儿子如何能让老爸如此侵犯隐私啊?咋办呢?不自动传递变更通知不就结了!为了实现手动通知,需要重写Son的类方法+automaticallyNotifiesObserversForKey,来告知obj-c你不想自动通知观察者自己的变化,注意是类方法哦!可以通过对特定的键名返回NO来完成:
1 +(BOOL)automaticallyNotifiesObserversForKey:(NSString *)key{ 2 if([key isEqualToString:@"girl_friend_name"]) 3 return NO; 4 return YES; 5 }
在Son类的实现中加入以上类方法,重新编译运行:
1 apple@kissAir: objc_src$./k 2 3 2014-07-06 20:37:36.726 k[2180:507] My love girl is lili ... Does baba know? 4 5 2014-07-06 20:37:36.729 k[2180:507] Yes , It's first time to induction son's girl_name!!! 6 7 2014-07-06 20:37:36.729 k[2180:507] My love girl is mimi ... Does baba know? 8 9 2014-07-06 20:37:36.730 k[2180:507] My love girl is libinbin ... Does baba know?
老爸”感应“不到喽!但是这样长了老爸会怀疑的啦,肿么宝贝儿子从不谈恋爱呢?这个不行哦!于是乎聪明的hack儿子可以再手动的通知老爸篡改后的内容啊!具体为:在Son属性写者方法的变化之前调用-willChangeValueForKey,然后在变化之后调用-didChangeValueForKey,重新改写写者方法吧,修改后完整代码如下:
1 #import <Foundation/Foundation.h> 2 3 #define msg(...) NSLog(__VA_ARGS__) 4 #define mki(x) [NSNumber numberWithInt:x] 5 6 @interface Son:NSObject{ 7 NSString *girl_friend_name; 8 } 9 //在属性默认为atomic的情况下,如果自定义写者方法必须同时自定义读者方法, 10 //或者将属性改为nonatomic模式。 11 @property(nonatomic) NSString *girl_friend_name; 12 -(void)think; 13 -(void)fall_in_love_with:(NSString *)girl_name; 14 @end 15 16 @implementation Son 17 @synthesize girl_friend_name; 18 19 -(void)fall_in_love_with:(NSString *)girl_name{ 20 self.girl_friend_name = girl_name; 21 } 22 23 -(void)think{ 24 msg(@"My love girl is %@ ... Does baba know?",girl_friend_name); 25 } 26 27 +(BOOL)automaticallyNotifiesObserversForKey:(NSString *)key{ 28 if([key isEqualToString:@"girl_friend_name"]) 29 return NO; 30 return YES; 31 } 32 33 -(void)setGirl_friend_name:(NSString *)name{ 34 [self willChangeValueForKey:@"girl_friend_name"]; 35 //让老爸以为自己和女学霸谈恋爱哦 36 girl_friend_name = @"女学霸"; 37 [self didChangeValueForKey:@"girl_friend_name"]; 38 //恢复本色吧,BAD BOY... :) 39 girl_friend_name = name; 40 } 41 @end 42 43 @interface Baba:NSObject{ 44 Son *son; 45 } 46 @property Son *son; 47 @end 48 49 @implementation Baba 50 @synthesize son; 51 52 -(id)initWithSon:(Son *)son_v{ 53 self = [super init]; 54 if(self){ 55 son = son_v; 56 [son addObserver:self forKeyPath:@"girl_friend_name" \ 57 options:(NSKeyValueObservingOptionNew|\ 58 NSKeyValueObservingOptionOld|NSKeyValueObservingOptionInitial) \ 59 context:nil]; 60 } 61 return self; 62 } 63 64 -(void)observeValueForKeyPath:(NSString *)key_path \ 65 ofObject:(id)obj change:(NSDictionary *)change \ 66 context:(void *)context{ 67 68 NSString *old_name = [change objectForKey:NSKeyValueChangeOldKey]; 69 NSString *new_name = [change objectForKey:NSKeyValueChangeNewKey]; 70 71 if(!old_name){ 72 //NSKeyValueObservingOptionInitial选项的作用,第一次hook会发一次消息 73 //以后每次更改会发一次消息。 74 msg(@"Yes , It's first time to induction son's girl_name!!!"); 75 }else{ 76 if([new_name isEqualToString:@"libinbin"]){ 77 [obj fall_in_love_with:@"fanbinbin"]; 78 } 79 msg(@"My son's %@ change from %@ to %@ ... That's OK? :(",\ 80 key_path,old_name,new_name); 81 } 82 } 83 84 -(void)dealloc{ 85 [son removeObserver:self forKeyPath:@"girl_friend_name"]; 86 son = nil; 87 //[super dealloc]; 88 } 89 @end 90 91 int main(int argc,char *argv[]) 92 { 93 @autoreleasepool{ 94 Son *son = [[Son alloc] init]; 95 [son fall_in_love_with:@"lili"]; 96 [son think]; 97 98 Baba *baba = [[Baba alloc] initWithSon:son]; 99 [son fall_in_love_with:@"mimi"]; 100 [son think]; 101 102 //儿子本来喜欢的是libinbin,可是... 103 [son fall_in_love_with:@"libinbin"]; 104 //被他老爸用特异功能改成fanbinbin啦! 105 [son think]; 106 } 107 return 0; 108 }
编译运行结果如下:
apple@kissAir: objc_src$./k 2014-07-06 20:58:59.284 k[2270:507] My love girl is lili ... Does baba know? 2014-07-06 20:58:59.286 k[2270:507] Yes , It's first time to induction son's girl_name!!! 2014-07-06 20:58:59.287 k[2270:507] My son's girl_friend_name change from lili to 女学霸 ... That's OK? :( 2014-07-06 20:58:59.287 k[2270:507] My love girl is mimi ... Does baba know? 2014-07-06 20:58:59.288 k[2270:507] My son's girl_friend_name change from mimi to 女学霸 ... That's OK? :( 2014-07-06 20:58:59.288 k[2270:507] My love girl is libinbin ... Does baba know?
这回该老爸傻傻的以为儿子一直在和女学霸贪恋喽,各位童鞋当年也是这样糊弄老爸老妈的吧,嘿嘿。