NSUndoManager won't undo editing of a NSMutableDictionary

Posted by xon1c on Stack Overflow See other posts from Stack Overflow or by xon1c
Published on 2010-05-26T19:26:39Z Indexed on 2010/05/26 19:41 UTC
Read the original article Hit count: 281

Filed under:
|
|
|

Hi,

I'm experiencing problems with the undo operation. The following code won't undo an removeObjectForKey: operation but the redo operation setObject:ForKey: works.

 - (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 allKeys] objectAtIndex:0], thePath);

 [colorsForPaths removeObjectForKey:thePath];

}

The output on the console looks like:

// Before setObject:ForKey:
Object id is: 1184384 and Key id is: 1530016

// Before removeObjectForKey:
UNDO
Object id is: 2413664 and Key id is: 1530016

I don't get why the Object id is different although the Key id remains the same. Is there some special undo/redo handling of NSMutableDictionary objects?

thx xonic

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa