Question

I have an NSArrayController who's content array is bound to File's Owner (my window controller) with the model key path is set to chartData.events chartData is a subclass of NSObject and events is a synthesized NSMutableArray property.

chartData needs to know when the NSArrayController adds and removes event objects to the events property. I thought I should do this by implementing the mutable indexed accessors. I put this into my chartData class implementation:

- (void)insertObject:(TNLEvent *)event inEventsAtIndex:(NSUInteger)index {
    [self.events insertObject:event atIndex:index];
    return;
}

- (void)insertEvents:(NSArray *)eventArray atIndexes:(NSIndexSet *)indexes {
    [self.events insertObjects:eventArray atIndexes:indexes];
    return;
}

But neither of those methods are called when the NSArrayController's add: message is sent. I assume that the key path that is used by the NSArrayController does not map to those methods, or else they are not key-value compliant. How can I know when objects are added to or removed from events?

UPDATE: I overrode the synthesized accessor setEvents and found out that the array controller uses this method every time is adds an object to the array that it controls. Thus, the mutable indexed accessors are never even used. This doesn't help me though because I still won't know which object has been added.

Was it helpful?

Solution

My solution was correct, but I didn't implement all of the necessary accessors.

This appears to be the minimum set of accessors that should be implemented.

- (void)insertObject:(id)object inEventsAtIndex:(NSUInteger)index;
- (id)objectInEventsAtIndex:(NSUInteger)index;
- (NSArray *)eventsAtIndexes:(NSIndexSet *)indexes;
- (void)removeObjectFromEventsAtIndex:(NSUInteger)index;

If these are in place the array controller will use them instead of the setter getter for the array attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top