cocoa 键值观察依赖键(KVO)--笔录

前端之家收集整理的这篇文章主要介绍了cocoa 键值观察依赖键(KVO)--笔录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

引子

1. 什么是键值观察依赖键,可参考文章[深入浅出Cocoa]详解键值观察(KVO)及其实现机理中第4点。

2.由于本人在学习[深入浅出Cocoa]详解键值观察(KVO)及其实现机理过程中,对键值观察依赖键有些不解,固做此笔录。

键值观察依赖键的简单代码实现

我们在上一篇cocoa 自动键值观察(KVO)--笔录 的基础上,对其代码进行修改


1.添加Other.h 和Other.m文件,目的是想把Other对象的num键作为Target对象age键的依赖键。

  1. //
  2. // Other.h
  3. // test_kvo_can_del
  4. //
  5. // Created by fenglh on 14-7-31.
  6. // Copyright (c) 2014年 yons. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface Other : NSObject
  12. @property int num; //添加变量num,作为依赖键
  13. @end

  1. //
  2. // Other.m
  3. // test_kvo_can_del
  4. //
  5. // Created by fenglh on 14-7-31.
  6. // Copyright (c) 2014年 yons. All rights reserved.
  7. //
  8.  
  9. #import "Other.h"
  10.  
  11. @implementation Other
  12. @synthesize num;
  13.  
  14. @end

2.修改Target类

1) 添加other指针,并合成访问器。

  1. //
  2. // Target.h
  3. // AutoKVO
  4. //
  5. // Created by fenglh on 14-7-31.
  6. // Copyright (c) 2014年 yons. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "Other.h"
  11.  
  12. @interface Target : NSObject
  13. @property int age; // for auto KVO
  14. @property Other * other; //新添加指针,这个指针必须要有
  15.  
  16. @end

在被观察者Targer类中添加如下类方法(注意,方法的名字是不固定的),作用是指明依赖关系。即Age属性依赖于other的num属性

  1. <pre name="code" class="objc">//
  2. // Target.m
  3. // AutoKVO
  4. //
  5. // Created by fenglh on 14-7-31.
  6. // Copyright (c) 2014年 yons. All rights reserved.
  7. //
  8.  
  9. #import "Target.h"
  10.  
  11. @implementation Target
  12. @synthesize age; // for auto KVO
  13. @synthesize other;
  14.  
  15. + (NSSet *)keyPathsForValuesAffectingAge
  16. {
  17. NSSet * keyPaths = [NSSet setWithObjects:@"other.num",nil]; //注意这里的other.num 写法是严格要求的,名字要与头文件声明的指针other一致!
  18. return keyPaths;
  19. }
  20.  
  21. @end


  1.  

注:other.num 中的other不是随便写的,是要遵循那个什么KVC-Compliant 规则的, 这里的“other”名字要与头文件声明的"other"指针的名字一致!



3.修改main函数

  1. //
  2. // main.m
  3. // AutoKVO
  4. //
  5. // Created by fenglh on 14-7-31.
  6. // Copyright (c) 2014年 yons. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "Observer.h"
  11. #import "Target.h"
  12. #import "Other.h"
  13. int main(int argc,const char * argv[])
  14. {
  15. @autoreleasepool {
  16. Observer *observer = [[[Observer alloc] init] autorelease];
  1. <span style="white-space:pre"></span><pre name="code" class="objc" style="font-size: 11px; text-align: left; "><span style="white-space:pre"> </span>Other * other = [[[Other alloc] init] autorelease]; //新增1
Target * target = [[[Target alloc] init] autorelease];
  1.  
  1. <span style="white-space:pre"> </span>//这里用来初始化,target.other的值。最好的办法,我们可以重写Target类的init方法,直接构造对象的时候就初始other的值了。
  2. target.other = other; //新增2
  3. [target.other retain];//新增3
  4. [target addObserver:observer forKeyPath:@"age" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:[Target class]];
  5. //[target setAge:500]; //注释掉原有的
  6. [other setNum:5000]; //新增4
  7. [target removeObserver:observer forKeyPath:@"age"];
  8. }
  9. return 0;
  10. }
上面只是新增了4处代码

1)1是实例化一个other对象

2)2和3只是为了初始化other对象

3)4调用other对象的setNum方法,即修改依赖键num的值


4.运行代码输出结果:

  1. 2014-07-31 07:01:34.839 test_kvo_can_del[28802:303] >> class : [Target],Age changed
  2. 2014-07-31 07:01:34.840 test_kvo_can_del[28802:303] >> old age is 0
  3. 2014-07-31 07:01:34.842 test_kvo_can_del[28802:303] >> old age is 0

可以看到当我们对other对象的num值进行修改时(注意,我们的被观察者是target而不是other),仍然会触发Observer的observeValueForKeyPath 函数来处理变更通知

所以,这就是键值观察依赖键的作用了。(至于为什么输出都是0,是随便打印的)


5.小结:

实现键值观察依赖键实现要素:

1)实现keyPathsForValuesAffectingXXXX 的方法,来指明依赖键是那些。

2)在被观察者类Target加入一个指向依赖键所在类Other的指针 ,如Target类中添加@property Other * other; //添加指针

3)第2点所说的指针名字“other”要与keyPathsForValuesAffectingXXXX方法里面的other名字一致!否则不遵循KVO-Compliant准则。



注:代码不算完整,只是用来验证键值观察依赖键的实现。应该还有许多不足之处,多多指教。


参考文章

[深入浅出Cocoa]详解键值观察(KVO)及其实现机理

猜你在找的设计模式相关文章