前言
刚写oc,还不知道kvc,只知道这个东西和前端中的json很像,后来发现确实很像哈哈,不错还有一些前端json不具备的东西。
文章内容整理自KVC 与 KVO 理解,一篇2012年的文章,但是内容写的真的好,心怀敬畏。
一、KVC
KVC,即是指 NSKeyValueCoding,一个非正式的 Protocol,提供一种机制来间接访问对象的属性。KVO 就是基于 KVC 实现的关键技术之一。
一个对象拥有某些属性。比如说,一个 Person 对象有一个 name 和一个 address 属性。以 KVC 说法,Person 对象分别有一个 value 对应他的 name 和 address 的 key。 key 只是一个字符串,它对应的值可以是任意类型的对象。从最基础的层次上看,KVC 有两个方法:一个是设置 key 的值,另一个是获取 key 的值。如下面的例子:
//方法实现
- (void)changName:(card *)card newName:(NSString *)newName
{
NSString *oldName = [card valueForKey:@"name"];
[card setValue:newName forKey:@"name"];
NSString *Name = [card valueForKey:@"name"];
NSLog(@"change name from %@ to %@",oldName,Name);
}
//方法调用
card *BMW = [card new];
BMW.name = @"宝马";
[self changName:BMW newName:@"奔驰"];
//调用结果
textview[1668:192219] change name from 宝马 to 奔驰
以上代码采用valueForKey和setValue forKey 分别来获取value和改变value,讲card的name从宝马改到奔驰。
key和keyPat的区别
抱歉我搞错了card和car,本来是用car,毕竟例子都用了宝马和奔驰
如果card中有个key本身是一个对象,对象还有一个属性,像这样
@interface card : NSObject
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) Engine *Engine;
@end
@interface Engine : NSObject
@property (nonatomic,strong) NSString *EngineName;
@end
那么我们可以这样使用
- (void)logKey:(card *)card
{
NSString *name = [card valueForKey:@"name"];
//NSString *Engine = [[card valueForKey:@"Engine"] valueForKey:@"EngineName"];
NSString *Engine = [card valueForKeyPath:@"Engine.EngineName"];
NSLog(@"name = %@,Engine = %@",name,Engine);
}
上段代码中注释掉的那行和下面一样代码作用相同。
打印出来是这样的
textview[1890:211955] name = 奔驰,Engine = V10
二、KVO
我们想来创建一个cardWatch作为card的监听,只实现一个小小小的功能,当name改变时打印出来(模仿原博客,但是很多地方我实现的不好就改的简单了。)
//定义,将注册监听暴露出来
#import <Foundation/Foundation.h>
#import "card.h"
@interface cardWatch : NSObject
-(void) watchPersonForChangeOfAddress:(card *)p;
@end
//实现,数组这里的作用我猜作者是想保留到注册的监听的对象,可以实现注销
@interface cardWatch ()
{
NSMutableArray *m_observedPeople;
}
@end
@implementation cardWatch
-(void) watchPersonForChangeOfAddress:(card *)p
{
// this begins the observing
[p addObserver:self
forKeyPath:@"name"
options:0
context:nil];
// keep a record of all the people being observed,
// because we need to stop observing them in dealloc
[m_observedPeople addObject:p];
}
// whenever an observed key path changes, this method will be called
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// use the context to make sure this is a change in the address,
// because we may also be observing other things
NSLog(@"have a new name %@",[object valueForKey:@"name"]);
}
-(void) dealloc;
{
// must stop observing everything before this object is
// deallocated, otherwise it will cause crashes
for(card *p in m_observedPeople){
[p removeObserver:self forKeyPath:@"name"];
}
[m_observedPeople removeAllObjects];
m_observedPeople = nil;
// [super dealloc];
}
-(id) init;
{
if(self = [super init]){
m_observedPeople = [NSMutableArray new];
}
return self;
}
//这就是调用了啦
BMW.name = @"宝马";
cardWatch *watch = [cardWatch new];
[watch watchPersonForChangeOfAddress:BMW];
BMW.name = @"飞度";
BMW.name = @"思域";
BMW.name = @"手扶拖拉机";
//打印如我们想的一样,开心
2017-07-03 17:34:28.204 textview[2079:246465] have a new name 飞度
2017-07-03 17:34:28.204 textview[2079:246465] have a new name 思域
2017-07-03 17:34:28.205 textview[2079:246465] have a new name 手扶拖拉机
恩,基本上就是这样了,这个监听感觉和ios的广播有点像,这也是前端json不具备的,感觉KVO给了这个数据结构生命,很厉害。
网友评论