Skip to content

Instantly share code, notes, and snippets.

@zhangwc
Created September 5, 2013 03:17
Show Gist options
  • Save zhangwc/6445656 to your computer and use it in GitHub Desktop.
Save zhangwc/6445656 to your computer and use it in GitHub Desktop.
aBook = [[Book alloc] initWithBookNO:@"NO.21342413" bookName:@"清朝秘史" author:@"张家州" price:25.50];
[aBook addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
// 观察到变化(收到来自被观察者的消息通知)
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if([keyPath isEqual:@"price"]) {
NSLog(@">>>>>>>price is changed");
NSLog(@"old price is %@",[change objectForKey:@"old"]);
NSLog(@"new price is %@",[change objectForKey:@"new"]);
}
}
[aBook removeObserver:self forKeyPath:@"price"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment