Difference between mutableArrayValueForKey and calling insertObject:inEmployeesAtIndex: directly

Posted by jasonbogd on Stack Overflow See other posts from Stack Overflow or by jasonbogd
Published on 2010-06-12T05:32:30Z Indexed on 2010/06/12 5:43 UTC
Read the original article Hit count: 323

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa-bindings