你如何深拷贝一个多维的NSMutableArray及其内容? [英] How do you deep copy a multidimensional NSMutableArray and its contents?
问题描述
我现在有一个三维的NSMutableArray,我需要深拷贝。然而,现在看来,以下code使它和它的内容,当我试图从它删除任何对象成为不可变的,因为它会导致NSInvalidArgumentException。
I currently have a three dimensional NSMutableArray which I need to deep copy. However, it appears that the following code causes it and its contents to become immutable, since it causes NSInvalidArgumentException when I attempt to remove any objects from it.
NSMutableArray* copy = [[[NSMutableArray alloc] initWithArray:input copyItems:YES] autorelease];
我怎么能不深导致它成为不可改变复制一个数组?
How can I deep copy an array without causing it to become immutable?
推荐答案
从上市,
不变类的副本imlementation通常返回相同
对象 - 因为它是不可变的没有必要有一个真实
复制。但你不必担心这一点。
The copy imlementation of immutable classes usually returns the same object - because it's immutable there is no need to have a "real" copy. But you don't have to worry about this.
从以上 http://lists.apple.com/档案/可可开发/ 2008 /月/ msg00172.html
因此,请确保您添加在你的输入
数组,即输入数组转换成可变的副本,然后调用该方法。
So make sure before you add your input
array, convert that input array into a mutable copy and then call the method.
NSMutableArray* mutableInput = [input mutableCopy];
NSMutableArray* copy = [[[NSMutableArray alloc] initWithArray:mutableInput copyItems:YES]autorelease];
这篇关于你如何深拷贝一个多维的NSMutableArray及其内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!