深层定制的一个NSMutableArray的副本NSMutableArray中成员的对象
问题描述:
我试图做一个NSMutableArray
,其对象是与此类似一个自定义的类的实例的深层副本:深层定制的一个NSMutableArray的副本NSMutableArray中成员的对象
@interface CustomParent : NSObject
@property NSInteger Id;
@property (strong, nonatomic) NSString *IdStr;
@property (weak, nonatomic) NSDate *Date;
@property (strong, nonatomic) NSMutableArray *CustomChildren;
@property (strong, nonatomic) CustomType *Type;
@property float Value;
@end
我知道有很多处理复制对象的帖子,但我没有找到用集合成员或属性获取对象完整副本的示例。 NSMutableArray *dstArray = [[NSMutableArray alloc] initWithArray:srcArray copyItems:YES];
引发了涉及copyWithZone
方法的例外情况。
我该怎么做?谢谢!
答
为了深副本的阵列
[[NSMutableArray alloc] initWithArray:srcArray copyItems:YES];
将发送copyWithZone:
到集合内的每个对象的内容。如果他们没有回应这个选择器,你会得到一个崩溃。
让您的CustomParent
类符合NSCopying
协议,您就完成了。
这里有一个关于如何实现它的一些额外的信息:Implementing NSCopying
看看NSCoding协议:https://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/Protocols /NSCoding_Protocol/Reference/Reference.html –
你可能是指'NSCopying' –
@AppsDev,提出的解决方案的运气? –