@SanMao
2015-08-06T00:35:03.000000Z
字数 1506
阅读 1156
UI
// 字典转模型
[p setValuesForKeysWithDictionary:dict];
// 修改私有的成员变量
[p setValue:@"20" forKeyPath:@"age"];
// 给模型的简单属性赋值
[p setValue:@"rose" forKeyPath:@"name"];
// 给模型的复杂属性赋值
[p.dog setValue:@"wangcai" forKeyPath:@"name"];
[p setValue:@"wangcai" forKeyPath:@"dog.name"];
// 能修改私有成员变量
- (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;
// 如果valueForKeyPath:方法的调用者是数组,那么就是去访问数组元素的属性值
// 利用b对象来监听a对象name属性的改变
[a addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"test"];
* 在监听器中实现监听方法
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@ %@ %@ %@", object, keyPath, change, context);
}
// 为对象p添加一个观察者(监听器)
// Observer:观察者(监听器)
// KeyPath:属性名(需要监听哪个属性)
[p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"test"];
/**
* 当利用KVO监听到某个对象的属性值发生了改变,就会自动调用这个
*
* @param keyPath 哪个属性被改了
* @param object 哪个对象的属性被改了
* @param change 改成咋样
* @param context 当初addObserver时的context参数值
*/
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@ %@ %@ %@", object, keyPath, change, context);
}