1, KVO
KVO Introduction: Key value observing, through the key key to observe the object's corresponding key key property value whether or not changed.
KVO: The current object wants to listen for changes to a property of another object.
Kvo Use steps: ① to add listeners to an object's properties, ② listeners to implement the listener, and to remove the listener before the ③ object is destroyed.
The current object listens to one of the properties of another object:
[person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
Nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold, when the current object implements the listening method, the listening method change字典 can get the value of the other object before and after the change of the listener's property.
The current object implementation - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context方法 ;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ NSLog(@"%@对象的%@属性发生变化", object, keyPath); NSLog(@"%@", change);}
In the listening method, the corresponding key can be used to obtain the corresponding value in the change dictionary.
The current object listens for the property of the listener object must be before, the property of another object must change after;
Be sure to remove the listener before being destroyed by the listener;
Complete KVO Monitoring:
-(void) viewdidload { [Super viewdidload]; //1. Create Person Object Hfperson *p = [[Hfperson alloc] init]; //2. Register for monitoring [P addobserver:self forkeypath:@ "name" Options:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold context:nil]; //3. Change object Properties p.name = @ "kawaii"; //4. Remove Monitoring [P removeobserver:self forkeypath:@ "name"];} The/** listener implements a listening method @param keypath which listener listens on the property @param object which object is being monitored by the listener @param change the dictionary before and after the object property value changes @param context Object registers the listener when the incoming data (typically used for communication between objects) */-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: ( nsdictionary<nskeyvaluechangekey,id> *) Change context: (void *) context{ NSLog (@ "%@", change) ;}
KVO Example:
// cc监听了aa的name属性的改变[aa addObserver:cc forKeyPath:@"name" options: NSKeyValueObservingOptionOld context:nil];// cc得实现监听方法/*** 当监听到object的keyPath属性发生了改变*/- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{NSLog(@"监听到%@对象的%@属性发生了改变, %@", object, keyPath, change);}
2. Kvc\kvo Use scene:
"Bug Summary"
BUG-1: The listener has been destroyed, but no interception is removed;
解决方案: To remove the listener before being destroyed by the listener, you can try to remove listeners from the Dealloc method of the listener, but you may need to save the listener as an attribute to the listener, or use the proxy, block.
注意事项: When the listener is removed by the listener, the listener and the monitored properties must be identical to the registered listener, otherwise the program will crash.
iOS Core note-KVO mechanism