Grossiste
Grossiste

Reputation: 17

Copy NSObject with new memory allocation

I have a NSMutableArray that contains NSObject. I want to make a copy of it but when I modify the original array, the copy is too. I tried to implement copyWithZone like this:

-(id) copyWithZone: (NSZone *) zone

{

Play *playCopy = [[Play allocWithZone: zone] init];

[playCopy setName:name];

[playCopy setAValues:aValues];

return playCopy;

}

I call this function in a viewController:

NSMutableArray *aTmpPlay = [[NSMutableArray alloc] initWithArray:aPlays copyItems:YES];

What I need to do ? How I can just retrieve the value of the object and not the reference ?

Thanks for your help !

Upvotes: 0

Views: 334

Answers (1)

Silver Gonzales
Silver Gonzales

Reputation: 734

Essentially, you are still assigning the reference of the memory allocated for name and values, hence, it is still pointing to the same objects. call copy before assigning the name and aValues.

[playCopy setName:[name copy]];
[playCopy setAValues:[aValues copy]];

Upvotes: 1

Related Questions