KVC 后边附有源码
类似的用法
[user1 setValue:@"令狐冲" forKey:@"name"];
[user1 setValue:@"武当"forKeyPath:@"menpai.name"];//中间有路径过程
NSLog(@"menpai:%@",[user1 valueForKeyPath:@"menpai.name"]);
[user1 setValuesForKeysWithDictionary:dic];//dictionary和user1的属性要一致
/KVO
//self 去监听 user1.name 是否发生变化
[user1 addObserver:selfforKeyPath:@"name"options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNewcontext:NULL];
user1.name = @"任盈盈";
//如果user1.name 发生变化就会调用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@"%@的值变成了%@",keyPath,[change objectForKey:@"new"]);
}
源码:http://download.csdn.net/detail/chenscda/7135107