Key-Value Coding (KVC)
KVC,即是指 NSKeyValueCoding,一個非正式的 Protocol,提供一種機制來間接訪問對象的屬性。KVO 就是基於 KVC 實現的關鍵技術之一。
一個對象擁有某些屬性。比如說,一個 Person 對象有一個 name 和一個 address 屬性。以 KVC 說法,Person 對象分別有一個 value 對應他的 name 和 address 的 key。 key 只是一個字符串,它對應的值可以是任意類型的對象。從最基礎的層次上看,KVC 有兩個方法:一個是設置 key 的值,另一個是獲取 key 的值。如下面的例子:
1
2
3
4
5
6
7
8
9
10
11
12
|
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 可以這樣寫:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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" ]; |
Key-Value Observing (KVO)
Key-Value Observing (KVO) 建立在 KVC 之上,它能夠觀察一個對象的 KVC key path 值的變化。舉個例子,用代碼觀察一個 person 對象的 address 變化,以下是實現的三個方法:
- watchPersonForChangeOfAddress: 實現觀察
- observeValueForKeyPath:ofObject:change:context: 在被觀察的 key path 的值變化時調用。
- dealloc 停止觀察
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
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 觀察對象的值,當值發生變化的時候會收到通知。
總結:
KVC的常用方法:
- (id)valueForKey:(NSString *)key; -(void)setValue:(id)value forKey:(NSString *)key;
valueForKey的方法根據key的值讀取對象的屬性,setValue:forKey:是根據key的值來寫對象的屬性。
注意:
1. key的值必須正確,如果拼寫錯誤,會出現異常
2. 當key的值是沒有定義的,valueForUndefinedKey:這個方法會被調用,如果你自己寫了這個方法,key的值出錯就會調用到這裏來
3. 因爲類key反覆嵌套,所以有個keyPath的概念,keyPath就是用.號來把一個一個key鏈接起來,這樣就可以根據這個路徑訪問下去
4. NSArray/NSSet等都支持KVC
KVO的 優勢 :
1.能夠提供一種簡單的方法實現兩個對象間的同步。例如:model和view之間同步;
2.能夠對非我們創建的對象,即內部對象的狀態改變作出響應,而且不需要改變內部對象(SKD對象)的實現;
3.能夠提供觀察的屬性的最新值以及先前值;
4.用key paths來觀察屬性,因此也可以觀察嵌套對象;
5.完成了對觀察對象的抽象,因爲不需要額外的代碼來允許觀察值能夠被觀察
缺點 :
1.我們觀察的屬性必須使用strings來定義。因此在編譯器不會出現警告以及檢查;
2.對屬性重構將導致我們的觀察代碼不再可用;
3.複雜的“IF”語句要求對象正在觀察多個值。這是因爲所有的觀察代碼通過一個方法來指向;
4.當釋放觀察者時不需要移除觀察者。