Reputation: 2170
The following is code for enabling undo from the book cocoa programming for OS X by Aaron Hillegas :
-(void)removeObjectFromEmployeesAtIndex:(NSUInteger)index
{
Person *p = [employees objectAtIndex:index];
NSLog(@"removing %@ from %@", p, employees);
// Add the inverse of this operation to the undo stack
NSUndoManager *undo = [self undoManager]; [[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index];
if (![undo isUndoing]) {
[undo setActionName:@"Remove Person"];
}
[employees removeObjectAtIndex:index];
}
While removing an employee , we push a command onto the undo stack to reinsert that employee into the array . But what guarantee is there that p wont have been released when the undo is invoked ?
Upvotes: 1
Views: 439
Reputation: 364
'p' will get retained when the NSInvocation is created by "[[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index]"
Upvotes: 2