KVC: Key Value Coding, 常见作用:给模型属性赋值
+ (instancetype)dealWithDict:(NSDictionary *)dict
{
TWDeal *deal = [[self alloc] init];
// deal.title = dict[@"title"];
// deal.icon = dict[@"icon"];
// deal.buyCount = dict[@"buyCount"];
// deal.price = dict[@"price"];
// KVC - Key Value Coding
[deal setValuesForKeysWithDictionary:dict];
return deal;
}
KVO: Key Value Observing, 常用作用:监听模型属性值的改变
self.p1 = [[TWPerson alloc] init];
self.p1.name = @"p1";
self.p1.name = @"pppp1";
[self.p1 addObserver:self forKeyPath:@"name" options: NSKeyValueObservingOptionOld context:nil];
- (void)dealloc
{
[self.p1 removeObserver:self forKeyPath:@"name"];
}
#pragma mark - KVO监听方法
/**
* 当监听到object的keyPath属性发生了改变
*/
//keyPath:属性名称
//object:被观察的对象
//change:变化前后的值都存储在change字典中
//context:注册观察者时,context传过来的值
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"监听到%@对象的%@属性发生了改变, %@", object, keyPath, [change valueForKey:NSKeyValueChangeNewKey]);
}