原路徑:https://magicalboy.com/kvc_and_kvo/
KVC 與 KVO 是 Objective C 的關(guān)鍵概念,個(gè)人認(rèn)為必須理解的東西,下面是實(shí)例講解。
Key-Value Coding (KVC)
KVC,即是指 NSKeyValueCoding,一個(gè)非正式的 Protocol,提供一種機(jī)制來(lái)間接訪問(wèn)對(duì)象的屬性。KVO 就是基于 KVC 實(shí)現(xiàn)的關(guān)鍵技術(shù)之一。
一個(gè)對(duì)象擁有某些屬性。比如說(shuō),一個(gè) Person 對(duì)象有一個(gè) name 和一個(gè) address 屬性。以 KVC 說(shuō)法,Person 對(duì)象分別有一個(gè) value 對(duì)應(yīng)他的 name 和 address 的 key。 key 只是一個(gè)字符串,它對(duì)應(yīng)的值可以是任意類型的對(duì)象。從最基礎(chǔ)的層次上看,KVC 有兩個(gè)方法:一個(gè)是設(shè)置 key 的值,另一個(gè)是獲取 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);
}
現(xiàn)在,如果 Person 有另外一個(gè) key 配偶(spouse),spouse 的 key 值是另一個(gè) Person 對(duì)象,用 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 要區(qū)分開(kāi)來(lái),key 可以從一個(gè)對(duì)象中獲取值,而 key path 可以將多個(gè) key 用點(diǎn)號(hào) “.” 分割連接起來(lái),比如:
[p valueForKeyPath:@"spouse.name"];
相當(dāng)于這樣……
[[p valueForKey:@"spouse"] valueForKey:@"name"];
好了,以上是 KVC 的基本知識(shí),接著看看 KVO。
Key-Value Observing (KVO)
Key-Value Observing (KVO) 建立在 KVC 之上,它能夠觀察一個(gè)對(duì)象的 KVC key path 值的變化。舉個(gè)例子,用代碼觀察一個(gè) person 對(duì)象的 address 變化,以下是實(shí)現(xiàn)的三個(gè)方法:
watchPersonForChangeOfAddress: 實(shí)現(xiàn)觀察
observeValueForKeyPath:ofObject:change:context: 在被觀察的 key path 的值變化時(shí)調(diào)用。
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 的作用,它通過(guò) key path 觀察對(duì)象的值,當(dāng)值發(fā)生變化的時(shí)候會(huì)收到通知。