以下是由Aaron Hillegas为OS X的书籍可可编程启用撤销的代码:
-(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];
}
在删除员工时,我们将命令推送到撤消堆栈以将该员工重新插入阵列。但是,当调用撤消时,有什么保证不会被释放?
答案 0 :(得分:2)
'p'将被保留“