Ios n复制自定义对象的数组
我有一个单例对象,它管理我的所有列表。我们称之为ListStore ListStore有一个可变数组,用于存储列表Ios n复制自定义对象的数组,ios,objective-c,arrays,nscopying,Ios,Objective C,Arrays,Nscopying,我有一个单例对象,它管理我的所有列表。我们称之为ListStore ListStore有一个可变数组,用于存储列表 @interface ListStore : NSObject @property (nonatomic, copy) NSMutableArray *lists; // an array of List objects end 列表有一个可变数组,用于存储内容 @interface Wanderlist : NSObject <NSCoding, NSCopyin
@interface ListStore : NSObject
@property (nonatomic, copy) NSMutableArray *lists; // an array of List objects
end
列表有一个可变数组,用于存储内容
@interface Wanderlist : NSObject <NSCoding, NSCopying>
@property (nonatomic, copy) NSMutableArray *things; // an array of Thing objects
@end
我最初认为,因为我将其复制到了newlist
,所以它的所有成员都会被正确地复制。我现在明白情况并非如此:我仍然看到“对象在枚举时发生了变异”错误,但这次它发生在列表上
我可以在设置中使用NSCopying,以便在我说:
[[ListStore sharedStore] copy];
它在列表中调用copyWithZone:
,这样我就可以在东西上copyWithZone:
我试着这样设置,但是copyWithZone:
没有被调用
我知道我可以简单地说NSArray*newList=[list.things copy]
,但至少我想更好地理解NSCopying。在提交此问题之前,我在SO的相关问题列表中单击了一个问题,并找到了我的解决方案
我想发布我的解决方案不会有什么坏处
与此相反:
NSArray *newLists = [[ListStore sharedStore] lists] copy];
我必须做:
NSArray *newLists = [[NSArray alloc] initWithArray:[[ListStore sharedStore] lists] copyItems:true];
发件人:
一旦我使用initWithArray:copyItems:,它会自动将copyWithZone发送到我的所有列表对象,然后我就可以在列表上手动执行copyWithZone。在提交此问题之前,我在SO的相关问题列表中单击了一个问题,并找到了我的解决方案
我想发布我的解决方案不会有什么坏处
与此相反:
NSArray *newLists = [[ListStore sharedStore] lists] copy];
我必须做:
NSArray *newLists = [[NSArray alloc] initWithArray:[[ListStore sharedStore] lists] copyItems:true];
发件人:
一旦我使用initWithArray:copyItems:,它会自动将copyWithZone发送到我的所有列表对象,然后我就可以在列表上手动执行copyWithZone