Proper way to copy a readonly NSMutableArray

Posted by Jon Hull on Stack Overflow See other posts from Stack Overflow or by Jon Hull
Published on 2010-06-06T09:38:03Z Indexed on 2010/06/06 9:42 UTC
Read the original article Hit count: 432

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?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa