zoukankan      html  css  js  c++  java
  • 转:KVC 与 KVO 理解

    KVC 与 KVO 理解

    On 2012 年 6 月 7 日, in iPhone, by donly

    KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲解。

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

    好了,以上是 KVC 的基本知识,接着看看 KVO。

    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
  • 相关阅读:
    LuoguP1016 旅行家的预算 (贪心)
    LuoguP2254 [NOI2005]瑰丽华尔兹 (单调队列优化DP)(用记忆化过了。。。)
    LuoguP2876 [USACO07JAN]解决问题Problem Solving (区间DP)(未完成)
    Luogu3275 [SCOI2011]糖果 (差分约束)
    Luogu1993 小K的农场 (差分约束)
    Luogu4427 [BJOI2018]求和 (树上差分)
    LuoguP1516 青蛙的约会 (Exgcd)
    POJ3903Stock Exchange (LIS)
    LuoguP1020 导弹拦截 (LIS)
    线性筛
  • 原文地址:https://www.cnblogs.com/stephen-init/p/5159793.html
Copyright © 2011-2022 走看看