I have a question regarding using KVO-compliant methods to insert/remove objects from an array. I’m working through Aaron Hillegass’ Cocoa Programming for Mac OS X and I saw the following line of code (in the insertObject:inEmployeesAtIndex: method:
[[undoManager prepareWithInvocationTarget:self] removeObjectFromEmployeesAtIndex:index];
Correct me if I’m wrong, but I always thought it was better to call mutableArrayValueForKey: and then removeObjectAtIndex:…so I tried changing the above line to this:
[[undoManager prepareWithInvocationTarget:[self mutableArrayValueForKey:@"employees"]] removeObjectAtIndex:index];
And it didn’t work. Can someone explain the difference and why the first line works but the second line doesn’t?
UPDATE: My removeObjectFromEmployeesAtIndex:index method is implemented to make my collection class (an instance of NSMutableArray) KVC-compliant. So ultimately, calling [[self mutableArrayValueForKey:@"employees"] removeObjectAtIndex:index]; should end up calling [self removeObjectFromEmployeesAtIndex:index];
In your update you say:
Unfortunately this is not correct not matter what is in your
removeObjectFromEmployeesAtIndex:method as NSMutableArray will never call any methods in your class. Since you seem to be trying to get undo/redo functionality you have to use a method likeremoveObjectFromEmployeesAtIndex:. Otherwise when you hit undo for adding an employee you will have no way to ‘redo’ adding that employee. You also could have issues with undo/redo for edits to individual employees. If you wanted to you could change the line in theremoveObjectFromEmployeesAtIndex:method that reads[employees removeObjectAtIndex:index];to[[self valueForKey:@"employees"] removeObjectAtIndex:index];or[self.employees removeObjectAtIndex:index];but there is really no reason to go this route.