Pad*_*215 0 objective-c nsmutabledictionary ios
需要通过以下方式制作NSMutableDictionary的副本:
NSMutableDictionary *newScoutingEventDictionary = [[[NSMutableDictionary alloc] initWithDictionary:self.scoutingEvent copyItems:YES] mutableCopy];
Run Code Online (Sandbox Code Playgroud)
但是当我尝试改变其中的数组时:
[[newScoutingEventDictionary objectForKey:@"myArray"] replaceObjectAtIndex:i withObject:appendedEntry];
Run Code Online (Sandbox Code Playgroud)
它给了我一个错误:
-[__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'
Run Code Online (Sandbox Code Playgroud)
如果我试图改变原来的NSMutableDictionary:
[[self.scoutingEvent objectForKey:@"myArray"] replaceObjectAtIndex:i withObject:appendedEntry];
Run Code Online (Sandbox Code Playgroud)
它运行得很好.为什么我的复制版本打破了它?
mutableCopy做一个浅的可变副本.它不会使字典中的不可变对象变得可变.
您需要实现 - 是的,实现因为它不是API的一部分,原因有很多 - 深度可变复制,如果这是您需要的.
或者,如果您的字典可以表示为属性列表,则可以将其转换为一个,然后使用各种选项参数在取消存档时生成可变集合和/或离开.
| 归档时间: |
|
| 查看次数: |
839 次 |
| 最近记录: |