对KVC和KVO的理解
?
對KVC和KVO的理解
對KVC和KVO的理解
kvc kvo
KVC
KVC是KeyValueCoding的簡稱,它是一種可以直接通過字符串的名字(key)來訪問類屬性的機制。而不是通過調(diào)用Setter、Getter方法訪問。
KVC實例
一個對象擁有某些屬性。比如說,一個Person對象有一個name和一個address屬性。以KVC為例,Person對象分別有一個value對應(yīng)他的name和address的key。key只是一個字符串,它對應(yīng)的值可以是任意類型的對象。從最基礎(chǔ)的層次上看,KVC有兩個方法:一個是設(shè)置 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);}*
現(xiàn)在,如果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與keypath要區(qū)分開來,key 可以從一個對象中獲取值,而 keyPath可以將多個key用點號 “.” 分割連接起來,比如:
[p valueForKeyPath:@"spouse.name"];
相當(dāng)于這樣…
[[p valueForKey:@"spouse"] valueForKey:@"name"];
KVO
KVO是KeyValueObserving的簡稱,它提供一種機制,當(dāng)指定的對象的屬性被修改后,則對象就會接受到通知。簡單的說就是每次指定的被觀察的對象的屬性被修改后,KVO就會自動通知相應(yīng)的觀察者了。
KVO實例
KeyValueObserving(KVO)建立在 KVC之上,它能夠觀察一個對象的KVC key path 值的變化。舉個例子,用代碼觀察一個person對象的address 變化,以下是實現(xiàn)的三個方法:
- watchPersonForChangeOfAddress: 實現(xiàn)觀察
- observeValueForKeyPath:ofObject:change:context: 在被觀察的 key path 的值變化時調(diào)用。
- dealloc 停止觀察
@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 觀察對象的值,當(dāng)值發(fā)生變化的時候會收到通知。
?
轉(zhuǎn)載于:https://www.cnblogs.com/AliliWl/p/4168105.html
總結(jié)
以上是生活随笔為你收集整理的对KVC和KVO的理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows socket之最简单的s
- 下一篇: HTTP—缓存