xcode KVC:Key Value Coding 键值编码

赋值

// 能修改私有成员变量

- (void)setValue:(id)value forKey:(NSString *)key;

- (void)setValue:(id)value forKeyPath:(NSString *)keyPath;

- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues;

 

取值

// 能取得私有成员变量的值

- (id)valueForKey:(NSString *)key;

- (id)valueForKeyPath:(NSString *)keyPath;

- (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;

 

KVO:Key Value Observing 键值监听

使用步骤:

    1>.添加监听器

    // 利用b对象来监听a对象name属性的改变

    [a addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"test"];

 

    2>.在监听器中实现监听方法

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change     context:(void *)context

    {

        NSLog(@"%@ %@ %@ %@", object, keyPath, change, context);

    }

posted @ 2016-06-06 22:42  与格律上  阅读(157)  评论(0编辑  收藏  举报