Leopard Properties and NSMutable Array

Bill Bumgarner bbum at mac.com
Sat Jan 5 17:53:59 PST 2008


On Jan 5, 2008, at 5:41 PM, Jonathan wrote:
> self. content = [[NSMutableArray alloc] init];
> [self. content addObject:[NSString string];
>
> But I get the an error that tells me that I'm trying to mutate an  
> immutable collection.  Why is this, is the setter calling a -copy  
> method which returns an immutable copy? If so, why have this  
> behaviour?

That is because the synthesized method sends -copy to the mutable  
array, yielding an immutable array.

> Which argument to the @property directive would give the same as
>
> - (void)setContent:(NSArray *)newArray;
> {
> 	if(content != newArray) {
> 		[content release];
> 		content = [[NSMutableArray alloc] initWithArray:newArray];
> 	}
> }

Which, unfortunately, is the alternative.   There is an enhancement  
request already to offer a mutablecopy keyword.

b.bum


More information about the MacOSX-dev mailing list