KVO overview and instance understanding
KVO Overview: KVO, that is, Key-Value Observing. It provides a mechanism to receive notifications when the attributes of a specified object are modified. In short, KVO automatically notifies the corresponding observer after the attribute of the specified object to be observed is modified.
Advantages of KVO: KVO provides automatic message notifications when attributes change. In this way, developers do not need to implement the solution by themselves: Send a message Notification every time the attribute changes. This is the biggest advantage of the KVO mechanism. This solution has been clearly defined and can be easily used with framework-level support. Developers do not need to add any code or design their own observer models, which can be used directly in the project. Secondly, the KVO architecture is very powerful, and it can easily support multiple observers to observe the same attribute and related values.
The procedure is as follows: 1. register, specify the properties of the observer-(void) addObserver :( NSObject *) anObserver forKeyPath :( NSString *) keyPath options :( NSKeyValueObservingOptions) options context :( void *) context keyPath is the attribute value to be observed. options shows you the selection of key value changes, while context facilitates the transmission of the data you need (note that this is a void type. implementation callback method-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary *) change context :( void *) context change stores some changed data, for example, the data before the change and the data after the change; for example If the context is not empty during registration, context can be received here. This function is automatically called as long as the properties or instance variables of the Monitored object change. 3. The callback method is triggered and the instance understands the code.
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController{ NSString *kkk; NSMutableDictionary *xxx; }- (IBAction)btntest:(id)sender { [self setValue:@"222" forKey:@"kkk"]; NSMutableDictionary *ccc = [[NSMutableDictionary alloc] init]; [ccc setObject:@"v" forKey:@"k"]; [self setValue:ccc forKey:@"xxx"];}- (void)viewDidLoad { [super viewDidLoad]; xxx = [[NSMutableDictionary alloc] init]; [self setValue:@"111" forKey:@"kkk"]; [self addObserver:self forKeyPath:@"kkk" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL]; NSMutableDictionary *zzz = [[NSMutableDictionary alloc] init]; [self setValue:zzz forKey:@"xxx"]; [self addObserver:self forKeyPath:@"xxx" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];}-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ if([keyPath isEqualToString:@"kkk"]) { NSString *old = [change valueForKey:@"old"]; NSString *new = [change valueForKey:@"new"]; NSLog(@"old=%@,new=%@", old, new); } if([keyPath isEqualToString:@"xxx"]) { NSMutableDictionary *old = [change valueForKey:@"old"]; NSMutableDictionary *new = [change valueForKey:@"new"]; NSLog(@"old=%ld,new=%ld,xxx=%ld", [old count], [new count], [xxx count]); }}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end