我有一个具有只读属性的对象,我正在尝试为其实现 NSCopying。它有一个名为“subConditions”的 mutableArray(其中包含“SubCondition”对象)。我将其设为只读是因为我希望调用者能够更改数组中的数据,但不能更改数组本身。在编写 -copyWithZone: 方法之前,这非常有效。
在摸索了一下之后,我设法得到了一些似乎有效的东西。我不确定这是否是最佳做法。这是我的 -copyWithZone: 方法的简化版本:
-(id)copyWithZone:(NSZone*)zone
{
Condition *copy = [[[self class]allocWithZone:zone]init];
NSArray *copiedArray = [[NSArray alloc]initWithArray:self.subConditions copyItems:YES];
[copy.subConditions setArray:copiedArray];
[copiedArray release];
return copy;
}
这是复制只读 mutableArray 的正确/最佳方法吗?