によってNSMutableDictionaryのコピーを作成する必要があります。コピーしたNSMutableDictionary変更できません
NSMutableDictionary *newScoutingEventDictionary = [[[NSMutableDictionary alloc] initWithDictionary:self.scoutingEvent copyItems:YES] mutableCopy];
が、私はそれに配列を変異しようとすると:
:[[newScoutingEventDictionary objectForKey:@"myArray"] replaceObjectAtIndex:i withObject:appendedEntry];
を、それは私にエラーを与えます
-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x964d650
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI replaceObjectAtIndex:withObject:]: unrecognized selector sent to instance 0x964d650'
元のNSMutableDictionaryを変更しようとすると:
[[self.scoutingEvent objectForKey:@"myArray"] replaceObjectAtIndex:i withObject:appendedEntry];
うまく動作します。なぜ私のコピーされたバージョンはそれを壊しているのです
私は、NSMutableDictionary * newScoutingEventDictionary = [[[NSMutableDictionary alloc] initWithDictionary:self.scoutingEvent copyItems:YES] mutableCopy];が実際に2つのコピーを作成し、そのうちの最初のものがリークしていることを指摘しておきます... –
er ... 。 どうして?私はそれを見ない/理解しない。 – Padin215
正確には、[基本メモリ管理規則](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//)から2つの新しいインスタンスを作成します。 apple_ref/doc/uid/20000994-BAJHFBGH)、「alloc」は1つのインスタンスが作成されることを意味し、「mutableCopy」は第2のインスタンスを意味します。 –