本文介绍了NSUndoManager是否保留其参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是用于启用Aaron Hillegas对OS X的可可编程的撤消的代码:
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];
}
在移除员工时,我们将一个命令推送到撤销堆栈, employee into数组。但是,当调用撤消时,是什么保证p不会被释放?
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 ?
推荐答案
NSInvocation是由[[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index]创建的
'p' will get retained when the NSInvocation is created by "[[undo prepareWithInvocationTarget:self] insertObject:p inEmployeesAtIndex:index]"
这篇关于NSUndoManager是否保留其参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!