0

元に戻す操作で問題が発生しています。次のコードは操作を元に戻せません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

キー ID は同じままですが、オブジェクト ID が異なる理由がわかりません。オブジェクトの特別な取り消し/やり直し処理はありNSMutableDictionaryますか?

thx ソニック

4

2 に答える 2

2

NSUndoManager がその仕事をしているように聞こえますが、値を削除するときは、辞書に NSBezierPath キーのオブジェクトがありません。私の推測では、 NSBezierPath を辞書キーとして使用するのは安全ではありません。キーとして使用している NSBezierPath オブジェクトは、おそらく割り当てと元に戻す間のどこかで変更さhashれています。これは、 に到達するまでにメソッドが異なりremoveFromDictionary:、意味のある辞書キーがなくなっていることを意味します。代わりに、何らかの形でベジエ パスに関連付けられている NSString または NSNumber を辞書キーとして使用してみてください。

于 2010-05-26T20:10:22.057 に答える
1

In one example you're logging the address of [currentPath objectAtIndex:0], and in the other you're logging the address of an arbitrary key (not even a value, but a random key) from the dictionary. There's no reason I can see in the code why these should be the same thing.

于 2010-05-26T19:38:56.277 に答える