[英]How do you deep copy a multidimensional NSMutableArray and its contents?
I currently have a three dimensional NSMutableArray which I need to deep copy. 我目前有一个3D NSMutableArray,需要深度复制。 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. 但是,以下代码似乎导致其及其内容变得不可变,因为当我尝试从中删除任何对象时,它会导致NSInvalidArgumentException。
NSMutableArray* copy = [[[NSMutableArray alloc] initWithArray:input copyItems:YES] autorelease];
How can I deep copy an array without causing it to become immutable? 如何在不使数组变成不变的情况下深度复制数组?
From the listing, 从清单中
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. 但是您不必为此担心。
above from http://lists.apple.com/archives/cocoa-dev/2008/May/msg00172.html 以上来自http://lists.apple.com/archives/cocoa-dev/2008/May/msg00172.html
So make sure before you add your input
array, convert that input array into a mutable copy and then call the method. 因此,请确保在添加input
数组之前,将该输入数组转换为可变副本,然后调用该方法。
NSMutableArray* mutableInput = [input mutableCopy];
NSMutableArray* copy = [[[NSMutableArray alloc] initWithArray:mutableInput copyItems:YES]autorelease];
Use NSCoding 使用NSCoding
in .h file @interface classname : NSObject 在.h文件中@interface类名:NSObject
in .m file 在.m文件中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.