我遇到了撤消操作的问题。以下代码不会撤消removeObjectForKey:
操作,但重做操作setObject:ForKey:
有效。
- (void) insertIntoDictionary:(NSBezierPath *)thePath
{
[[[window undoManager] prepareWithInvocationTarget:self] removeFromDictionary:thePath];
if(![[window undoManager] isUndoing])
[[window undoManager] setActionName:@"Save Path"];
NSLog(@"Object id is: %d and Key id is: %d", [currentPath objectAtIndex:0], thePath);
[colorsForPaths setObject:[currentPath objectAtIndex:0] forKey:thePath];
}
- (void) removeFromDictionary:(NSBezierPath *)thePath
{
[[[window undoManager] prepareWithInvocationTarget:self] insertIntoDictionary:thePath];
if(![[window undoManager] isUndoing])
[[window undoManager] setActionName:@"Delete Path"];
NSLog(@"Object id is: %d and Key id is: %d", [colorsForPaths objectForKey:thePath], thePath);
[colorsForPaths removeObjectForKey:thePath];
}
控制台上的输出如下所示:
// Before setObject:ForKey:
Object id is: 1183296 and Key id is: 1423872
// Before removeObjectForKey:
UNDO
Object id is: 0 and Key id is: 1423872
我不明白为什么 Object id 不同,尽管 Key id 保持不变。是否对对象进行了一些特殊的撤消/重做处理NSMutableDictionary
?
谢谢xonic