将 NSMutableDictionary 的内容复制到另一个 NSMutableDictionary 中?
我有一个 NSMutableDictionary,其中每个元素都是另一个字典。将其内容复制到另一个 NSMutableDictionary 的最佳方法是什么?我尝试过使用:
firstDictionary = [NSMutableDictionary dictionaryWithDictionary:secondDictionary];
但是不确定这是否是最好的方法。
I have an NSMutableDictionary
each element of which is another dictionary. What is the best way I can copy its contents into another NSMutableDictionary
? I've tried using:
firstDictionary = [NSMutableDictionary dictionaryWithDictionary:secondDictionary];
However not sure if this is the best way to do it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以使用 copy 和 mutableCopy 在可变和非可变字典之间跳转。
You can also jump between mutable and non-mutable dictionaries using copy and mutableCopy.
检查 NSDictionary initWithDictionary:copyItems: 方法。
它可以通过调用 item 类的 copyWithZone: 方法来深度复制元素。您必须自己在方法中复制字段。
Check the NSDictionary initWithDictionary:copyItems: method.
It it enables deep copying of elements thru calling copyWithZone: method of item's class. You will have to take care of copying the fields yourself within the method.
“最好”是什么意思?
无论如何,我在这里列出了一些方法:
[NSMutableDictionary
DictionaryWithDictionary:第二个字典];
initWithDictionary:第二个字典];
//不要忘记稍后
What do you mean by "best"?
Anyway, I listed some ways here:
[NSMutableDictionary
dictionaryWithDictionary:secondDictionary];
initWithDictionary:secondDictionary];
//don't forget to release later
遵守 NSCopying 协议,对每个对象执行 copyWithZone 操作。
如果 NsMutableDictionary 包含另一个字典,而该字典又包含另一个字典,那么你需要对所有级别的每个字典执行 copyWithZone 。
Conform to NSCopying Protocol and do copyWithZone on every object.
If NsMutableDictionary contains another dictionary, which contains another dictionary,, then you need to do copyWithZone on each dictionary at all levels.