详细理解KVC与KVO
在面试的时候,KVC与KVO有些时候还是会问到的,并且他们都是Objective C的关键概念,在这里我们先做一个简单地介绍:
(一)KVC:
KVC即指:NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性。KVO就是基于KVC实现的关键技术之一。
一个对象拥有某些属性;比如说,一个Person对象有一个name和一个address属性,以KVC说法,Person对象分别有一个value对应他的name和sex的key;这里key只是一个字符串,他对应的值可以使任意类型的对象;从最基础的从此上看,KVC有两个方法:一个是设置key的值,另一个是获取key的值;(示例代码如下:)
void changeName(Person *p, NSString *newName) { // using the KVC accessor (getter) method NSString *originalName = [p valueForKey:@"name"]; // using the KVC accessor (setter) method. [p setValue:newName forKey:@"name"]; NSLog(@"Changed %@'s name to: %@", originalName, newName); }
现在,如果 Person 有另外一个 key 配偶(spouse),spouse 的 key 值是另一个 Person 对象,用 KVC 可以这样写(示例代码如下:)
void logMarriage(Person *p) { // just using the accessor again, same as example above NSString *personsName = [p valueForKey:@"name"]; // this line is different, because it is using // a "key path" instead of a normal "key" NSString *spousesName = [p valueForKeyPath:@"spouse.name"]; NSLog(@"%@ is happily married to %@", personsName, spousesName); }
key 与 key pat 要区分开来,key 可以从一个对象中获取值,而 key path 可以将多个 key 用点号 “.” 分割连接起来,比如:
[p valueForKeyPath:@"spouse.name"];
相当于这样……
[[p valueForKey:@"spouse"] valueForKey:@"name"];
(二)KVO:
KVO即指:Key-Value Observing建立在 KVC 之上,它能够观察一个对象的 KVC key path 值的变化。举个例子,用代码观察一个 person 对象的 address 变化,以下是实现的三个方法:
(1)watchPersonForChangeOfAddress: 实现观察;
(2)observeValueForKeyPath:ofObject:change:context: 在被观察的 key path 的值变化时调用;
(3)dealloc 停止观察。
(示例代码如下:)
static NSString *const KVO_CONTEXT_ADDRESS_CHANGED = @"KVO_CONTEXT_ADDRESS_CHANGED" @implementation PersonWatcher -(void) watchPersonForChangeOfAddress:(Person *)p { // this begins the observing [p addObserver:self forKeyPath:@"address" options:0 context:KVO_CONTEXT_ADDRESS_CHANGED]; // 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 if(context == KVO_CONTEXT_ADDRESS_CHANGED) { NSString *name = [object valueForKey:@"name"]; NSString *address = [object valueForKey:@"address"]; NSLog(@"%@ has a new address: %@", name, address); } } -(void) dealloc; { // must stop observing everything before this object is // deallocated, otherwise it will cause crashes for(Person *p in m_observedPeople){ [p removeObserver:self forKeyPath:@"address"]; } [m_observedPeople release]; m_observedPeople = nil; [super dealloc]; } -(id) init; { if(self = [super init]){ m_observedPeople = [NSMutableArray new]; } return self; } @end
这就是 KVO 的作用,它通过 key path 观察对象的值,当值发生变化的时候会收到通知。
(以上便是KVO和KVC的相关理解,还希望大家相互补充共同进步)