OC文法之KVC與KVO,ockvckvo

來源:互聯網
上載者:User

OC文法之KVC與KVO,ockvckvo

1、Key-Value Coding (KVC)

KVC,即是指 NSKeyValueCoding,一個非正式的 Protocol,提供一種機制來間接訪問對象的屬性。KVO 就是基於 KVC 實現的關鍵技術之一。

一個對象擁有某些屬性。比如說,一個 Person 對象有一個 name 和一個 address 屬性。以 KVC 說法,Person 對象分別有一個 value 對應他的 name 和 address 的 key。 key 只是一個字串,它對應的值可以是任意類型的對象。從最基礎的層次上看,KVC 有兩個方法:一個是設定 key 的值,另一個是擷取 key 的值。如下面的例子:

123456789101112 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 可以這樣寫:

 

?
12345678910111213 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"];

好了,以上是 KVC 的基本知識,接著看看 KVO。

Key-Value Observing (KVO)

Key-Value Observing (KVO) 建立在 KVC 之上,它能夠觀察一個對象的 KVC key path 值的變化。舉個例子,用代碼觀察一個 person 對象的 address 變化,以下是實現的三個方法:

  • watchPersonForChangeOfAddress: 實現觀察
  • observeValueForKeyPath:ofObject:change:context: 在被觀察的 key path 的值變化時調用。
  • dealloc 停止觀察

 

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 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 觀察對象的值,當值發生變化的時候會收到通知。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.