Question

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 ?

Was it helpful?

Solution

'p' will get retained when the NSInvocation is created by "[[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index]"

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