xcode KVC:Key Value Coding 键值编码

时间:2022-11-30 08:15:26

赋值

// 能修改私有成员变量

- (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);

}