我创建了一个符合NSCopying
and的自定义类NSMutableCopying
。
-copyWithZone:
我已经为and添加了一个实现-mutableCopyWithZone:
,但是每当我调用-mutableCopy
我的对象并尝试调用另一个方法时,它就会崩溃,因为一些 ivars 已经变得不可变,即使我调用-mutableCopyWithZone:
了 ivars。
这是我复制课程的方式:
MyObject *flipped = [list mutableCopy];
[MyObject flip:flipped];
(代码在 上失败+flip:
,因为它试图在ivar上使用removeObjectAtIndex:
和)addObject:
NSMutableArray
这是我复制课程的方式:
- (id)mutableCopyWithZone:(NSZone *)zone {
id instance = nil;
if ((instance = [[[self class] alloc] init])) {
[instance setArray:[self.array mutableCopyWithZone:zone]];
[instance setObjects:[self.objects mutableCopyWithZone:zone]];
[instance setItemCount:self.itemCount];
}
return instance;
}
我不确定它为什么会失败,但我真的不明白为什么它不生成array
和objects
可变。
任何帮助表示赞赏。