国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

OC語法之KVC與KVO

2019-11-14 18:41:23
字體:
供稿:網(wǎng)友

1、Key-Value Coding (KVC)

KVC,即是指 NSKeyValueCoding,一個非正式的 PRotocol,提供一種機制來間接訪問對象的屬性。KVO 就是基于 KVC 實現(xiàn)的關鍵技術(shù)之一。

一個對象擁有某些屬性。比如說,一個 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);
 
}

現(xiàn)在,如果 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 要區(qū)分開來,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 變化,以下是實現(xiàn)的三個方法:

  • watchPersonForChangeOfAddress: 實現(xiàn)觀察
  • observeValueForKeyPath:ofObject:change:context: 在被觀察的 key path 的值變化時調(diào)用。
  • 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 觀察對象的值,當值發(fā)生變化的時候會收到通知。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 郧西县| 台湾省| 炉霍县| 邢台县| 疏勒县| 土默特左旗| 绵阳市| 阜阳市| 潮州市| 界首市| 巴马| 弥渡县| 衡阳市| 黑龙江省| 门源| 澄迈县| 焉耆| 古蔺县| 绿春县| 卓资县| 菏泽市| 潮州市| 全南县| 丰宁| 台中市| 田阳县| 郓城县| 柘荣县| 建瓯市| 依安县| 新闻| 安新县| 南溪县| 吉水县| 合肥市| 自贡市| 横山县| 漳浦县| 建昌县| 阿城市| 汽车|