KVC and KVO

来源:互联网 发布:linux系统启动命令 编辑:程序博客网 时间:2024/04/30 00:10

NSKeyValueCoding Protocol Reference

The NSKeyValueCoding informal protocol defines a mechanism by which you can access the properties of an object indirectly by name (or key), rather than directly through invocation of an accessor method or as instance variables. Thus, all of an object’s properties can be accessed in a consistent manner.

The basic methods for accessing an object’s values are setValue:forKey:, which sets the value for the property identified by the specified key, and valueForKey:, which returns the value for the property identified by the specified key. The default implementation uses the accessor methods normally implemented by objects (or to access instance variables directly if need be).

Getting Values

  • – valueForKey:
  • – valueForKeyPath:
  • – dictionaryWithValuesForKeys:
  • – valueForUndefinedKey:
  • – mutableArrayValueForKey:
  • – mutableArrayValueForKeyPath:
  • – mutableSetValueForKey:
  • – mutableSetValueForKeyPath:
  • – mutableOrderedSetValueForKey:
  • – mutableOrderedSetValueForKeyPath:

Setting Values

  • – setValue:forKeyPath:
  • – setValuesForKeysWithDictionary:
  • – setNilValueForKey:
  • – setValue:forKey:
  • – setValue:forUndefinedKey:

Changing Default Behavior

  • + accessInstanceVariablesDirectly

Validation

  • – validateValue:forKey:error:
  • – validateValue:forKeyPath:error:

NSKeyValueObserving Protocol Reference

he NSKeyValueObserving (KVO) informal protocol defines a mechanism that allows objects to be notified of changes to the specified properties of other objects.

You can observe any object properties including simple attributes, to-one relationships, and to-many relationships. Observers of to-many relationships are informed of the type of change made — as well as which objects are involved in the change.

NSObject provides an implementation of the NSKeyValueObserving protocol that provides an automatic observing capability for all objects. You can further refine notifications by disabling automatic observer notifications and implementing manual notifications using the methods in this protocol.



Change Notification

  • – observeValueForKeyPath:ofObject:change:context:

Registering for Observation

  • – addObserver:forKeyPath:options:context:
  • – removeObserver:forKeyPath:
  • – removeObserver:forKeyPath:context:

Notifying Observers of Changes

  • – willChangeValueForKey:
  • – didChangeValueForKey:
  • – willChange:valuesAtIndexes:forKey:
  • – didChange:valuesAtIndexes:forKey:
  • – willChangeValueForKey:withSetMutation:usingObjects:
  • – didChangeValueForKey:withSetMutation:usingObjects:

Observing Customization

  • + automaticallyNotifiesObserversForKey:
  • + keyPathsForValuesAffectingValueForKey:
  • – setObservationInfo:
  • – observationInfo

 kvc 就是一种通过字符串去间接操作对象属性的机制,  
访问一个对象属性我们可以 person.age  也可以通过kvc的方式   [person valueForKey:@"age"]
keypath 就是属性链式访问  如 person.address.street  有点象java里面的pojo  ognl表达式子类的

假如给出的字符串没有对象的属性 会访问valueForUndefineKey方法 默认实现是raise 一个异常 但你可以重写这个方法,   setValue的时候也是一样的道理

key path accounts.transactions.payee would return an array with all the payee objects, for all the transactions, in all the accounts.

当设置一个非对象属性为nil时会抛异常, 但你也可以重写方法

kvo 就是一个在语言框架层面实现的观察者模式 通过kvc的方式修改属性时,会主动通知观察者

例子
person类
@implementation Person

@synthesize name,age;


-(void) changeName{
    name=@"changeName directly";
}

@end


PersonMonitor类  监视了name属性

@implementation PersonMonitor


- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if ([keyPath isEqual:@"name"]) {
        NSLog(@"change happen, old:%@   new:%@",[change objectForKey:NSKeyValueChangeOldKey],[change objectForKey:NSKeyValueChangeNewKey]);

    }
    }

@end



测试代码
    Person *p =[[Person alloc] init];
   
    PersonMonitor *pm= [[PersonMonitor alloc]init];
    [p addObserver:pm forKeyPath:@"name" options:(NSKeyValueObservingOptionNew |
                                                  NSKeyValueObservingOptionOld) context:nil];
   
    NSLog(@"p.name is %@",p.name);
    [p setValue:@"name kvc" forKey:@"name"];
    NSLog(@"p name get by kvc is %@",[p valueForKey:@"name"]);
    p.name=@"name change by .name=";
    [p changeName]; //can't be notify


输出
2011-07-03 16:35:57.406 Cocoa[13970:903] p.name is name
2011-07-03 16:35:57.418 Cocoa[13970:903] change happen, old:name   new:name kvc
2011-07-03 16:35:57.420 Cocoa[13970:903] p name get by kvc is name kvc
2011-07-03 16:35:57.421 Cocoa[13970:903] change happen, old:name kvc   new:name change by .name=
最后一次修改是直接修改  所以没法产生通知


原创粉丝点击