Reputation: 46543
How do I observe the array controller's count?
What I've tried so far:
//...
self.array = [[NSMutableArray alloc] init];
self.arrayController = [[NSArrayController alloc] initWithContent:self.array];
[self.arrayController addObserver:self
forKeyPath:@"arrangedObjects"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:@"self.array"];
[self.array addObject:@"anoop"];
[self.array addObject:@"amit"];
NSLog(@"%ld", [self.arrayController.content count] );
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if( object == self.arrayController) {
...
}
}
My actual requirement is to observe the array count, but since Array is not KVO compliant, I can't do it. Also I do not want to subclass Array and just add one array property to it as shown in other answers. And then I thought of observing array controller.
Any leads will be highly appreciated.
Upvotes: 2
Views: 1433
Reputation: 6036
In order to observe the count of arrangedObjects
, you must specify a collection operator in your key path. The one you want to use is @count
.
So instead of using:
arrangedObjects
you should be using
arrangedObjects.@count
Upvotes: 4