To better understand KVO, I created a simple application with 1 button and 2 very basic model classes: Book and Author. I want to trigger the Book when the Author changes. For example, A simple KVO example, why doesn't this trigger the observer?
#import "AppDelegate.h"
#import "Book.h"
#import "Author.h"
@implementation AppDelegate {
Book *home;
Author *nancy;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSLog(@"FunWithKVO");
nancy = [[Author alloc] init];
[nancy setFirstName:@"Nancy"];
[nancy setLastName:@"Drew"];
home = [[Book alloc] init];
[home addObserver:nancy forKeyPath:@"lastName" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
[home setAuthor:@"Nancy Drew"];
}
- (IBAction)changeName:(id)sender {
NSLog(@"%@",[home author]);
[nancy setLastName:@"Martin"];
}
@end
NOW THIS SHOULD BE CALLED BUT ISN'T:
#import "Book.h"
@implementation Book
@synthesize author;
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
[author setValue:[NSString stringWithFormat:@"Nancy %@",[change value]]];
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
NSLog(@"name is now: %@",author);
}
@end
You mixed up the observing and the observed object in the registration.
nancy
is the object to be observed by home
, therefore it should be
[nancy addObserver:home forKeyPath:@"lastName" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];