KVC,即是指 NSKeyValueCoding,一個非正式的Protocol,提供一種機制來間接訪問對象的屬性。而不是通過調(diào)用Setter、Getter方法訪問。KVO 就是基于 KVC 實現(xiàn)的關鍵技術之一。
Demo:
@interface myPerson : NSObject
{
NSString*_name;
int _age;
int _height;
int _weight;
} @end
@interface testViewController :UIViewController
@property (nonatomic, retain) myPerson*testPerson;
@end
- (void)testKVC
{
testPerson = [[myPerson alloc] init];
NSLog(@"testPerson‘s init height =%@", [testPerson valueForKey:@"height"]);
[testPerson setValue:[NSNumber numberWithInt:168]forKey:@"height"]; NSLog(@"testPerson‘s height = %@", [testPerson valueForKey:@"height"]);
}
第一段代碼是定義了一個myPerson的類,這個類有一個_height的屬性,但是沒有提供任何getter/setter的訪問方法。同時在testViewController這個類里面有一個myPerson的對象指針。
當myPerson實例化后,常規(guī)來說是無法訪問這個對象的_height屬性的,不過通過KVC我們做到了,代碼就是testKVC這個函數(shù)。
運行之后打印值就是:
2015-3-13 11:16:21.970 test[408:c07] testPerson‘s init height = 0
2015-3-13 11:16:21.971 test[408:c07] testPerson‘s height = 168
這就說明確實讀寫了_height屬性。
KVC的常用方法:
- (id)valueForKey:(NSString *)key; -(void)setValue:(id)value forKey:(NSString *)key;
valueForKey的方法根據(jù)key的值讀取對象的屬性,setValue:forKey:是根據(jù)key的值來寫對象的屬性。
注意:
(1). key的值必須正確,如果拼寫錯誤,會出現(xiàn)異常
(2). 當key的值是沒有定義的,valueForUndefinedKey:這個方法會被調(diào)用,如果你自己寫了這個方法,key的值出錯就會調(diào)用到這里來
(3). 因為類key反復嵌套,所以有個keyPath的概念,keyPath就是用.號來把一個一個key鏈接起來,這樣就可以根據(jù)這個路徑訪問下去
(4). NSArray/NSSet等都支持KVC
2、KVO的是KeyValue Observe的縮寫,中文是鍵值觀察。這是一個典型的觀察者模式,觀察者在鍵值改變時會得到通知。iOS中有個Notification的機制,也可以獲得通知,但這個機制需要有個Center,相比之下KVO更加簡潔而直接。
KVO的使用也很簡單,就是簡單的3步。
1.注冊需要觀察的對象的屬性addObserver:forKeyPath:options:context:
2.實現(xiàn)observeValueForKeyPath:ofObject:change:context:方法,這個方法當觀察的屬性變化時會自動調(diào)用
3.取消注冊觀察removeObserver:forKeyPath:context:
Demo:
@interface myPerson : NSObject
{
NSString *_name;
int _age;
int _height;
int _weight;
}
@end
@interface testViewController : UIViewController
@property (nonatomic, retain) myPerson *testPerson;
- (IBAction)onBtnTest:(id)sender;
@end
- (void)testKVO
{
testPerson = [[myPerson alloc] init];
[testPerson addObserver:self forKeyPath:@"height" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"height"]) {
NSLog(@"Height is changed! new=%@", [change valueForKey:NSKeyValueChangeNewKey]);
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (IBAction)onBtnTest:(id)sender {
int h = [[testPerson valueForKey:@"height"] intValue];
[testPerson setValue:[NSNumber numberWithInt:h+1] forKey:@"height"];
NSLog(@"person height=%@", [testPerson valueForKey:@"height"]);
}
- (void)dealloc
{
[testPerson removeObserver:self forKeyPath:@"height" context:nil];
[super dealloc];
}
第一段代碼聲明了myPerson類,里面有個_height的屬性。在testViewController有一個testPerson的對象指針。
在testKVO這個方法里面,我們注冊了testPerson這個對象height屬性的觀察,這樣當testPerson的height屬性變化時, 會得到通知。在這個方法中還通過NSKeyValueObservingOptionNew這個參數(shù)要求把新值在dictionary中傳遞過來。
重寫了observeValueForKeyPath:ofObject:change:context:方法,這個方法里的change這個NSDictionary對象包含了相應的值。
需要強調(diào)的是KVO的回調(diào)要被調(diào)用,屬性必須是通過KVC的方法來修改的,如果是調(diào)用類的其他方法來修改屬性,這個觀察者是不會得到通知的。
轉(zhuǎn)載http://www.mamicode.com/info-detail-515516.html