//监听-(instancetype)init{ self = [super init]; if (self) { self.name = @"帅帅哒"; //注册监听// NSKeyValueObservingOptionNew// NSKeyValueObservingOptionOld [self addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL]; } return self; }//监听回调方法-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context
{
NSLog(@"新值:%@!",change[@"new"]);
NSLog(@"旧值:%@!",change[@"old"]);
}
//移除监听
-(void)dealloc
{
[self removeObserver:self forKeyPath:@"name"];
}
main.m
Person *person = [[Person alloc]init];
person.name = @"😡";
网友评论