以下は、AaronHillegasによるOSX用の本cocoaプログラミングからの取り消しを有効にするためのコードです。
-(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];
}
従業員を削除しているときに、コマンドをUNDOスタックにプッシュして、その従業員を配列に再挿入します。しかし、undoが呼び出されたときにpが解放されないという保証はありますか?