Proper way to copy a readonly NSMutableArray
- by Jon Hull
I have an object with a readonly property that I am trying to implement NSCopying for. It has a mutableArray called "subConditions" (which holds "SubCondition" objects). I have made it readonly because I want callers to be able to change the data in the array, but not the array itself. This worked really well until it was time to write the -copyWithZone: method.
After fumbling around a bit, I managed to get something that seems to work. I am not sure if it is the best practice though. Here is a simplified version of my -copyWithZone: method:
-(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;
}
Is this the correct/best way to copy a readonly mutableArray?