In the book we set BNRObserver as an observer for lastTime property
__unused BNRObserver *observer = [[BNRObserver alloc] init];
// I want to know the new value and the old value whenever lastTime is changed
[logger addObserver:observer
forKeyPath:@"lastTime"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:nil];
in BNRObserver.m file we implemented this as the book says this method will get called
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
NSString *oldValue = [change objectForKey:NSKeyValueChangeOldKey];
NSString *newValue = [change objectForKey:NSKeyValueChangeNewKey];
NSLog(@"Observed: %@ of %@ was changed from %@ to %@",
keyPath, object, oldValue, newValue);
}
But how do we know that this method will be called at the first place ? can someone please explain?