I have a NSMutableArray property in my AppDelegate called blocks. I would like to observe whenever an object is added to this array. I've read other posts, but I can't understand why this isn't working.
In my app delegate class, I implement
- (void)insertObject:(id)obj inBlocksAtIndex:(NSInteger)index
{
[blocks insertObject:obj atIndex:index];
}
In my view controller's init method, I add an observer to my AppDelegate reference.
boardModel = [[UIApplication sharedApplication] delegate];
[boardModel addObserver:self forKeyPath:@"blocks" options:0 context:NULL];
In my view controller's viewDidLoad method, I try invoking the KVO Indexed array accessor I implemented previously,
[boardModel insertObject:[[Block alloc] init] inBlocksAtIndex:0];
Then I implement my observeValueForKeyPath method:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"blocks"])
{
NSLog(@"ADDED");
}
}
I've tried adding an NSLog statement before the if statement in observeValueForKeyPath, and it seems as if it's never being called.
I've also tried NSLogging [[boardModel blocks] count], and it says the count is 1 (the object is being added).
I must be missing something.