MutableCopyWithZone, copy NSMutableArray?

I'm trying to make a mutableCopy planet object that contains 5 instance variables (one of them is NSMutableArray string literals. My problem is that I'm not sure how to set newPlanet> data to a copy of self> data, does this make sense?

-(id) mutableCopyWithZone: (NSZone *) zone {
    Planet *newPlanet = [[Planet allocWithZone:zone] init];
    NSLog(@"_mutableCopy: %@", [newPlanet self]);
    [newPlanet setName:name];
    [newPlanet setType:type];
    [newPlanet setMass:mass];
    [newPlanet setIndex:index];

    // NSMutableArray *data; HOW TO: newPlanet>data = self>data? 

    return(newPlanet);
}

EDIT_001:

Based on the comments of Chuck and bbum, I updated my method and added the following ...

@property(retain) NSMutableArray *data;
@synthesize data;

.

-(id) mutableCopyWithZone: (NSZone *) zone {
    Planet *newPlanet = [[Planet allocWithZone:zone] init];
    NSLog(@"_mutableCopy: %@", [newPlanet self]);
    [newPlanet setName:name];
    [newPlanet setType:type];
    [newPlanet setMass:mass];
    [newPlanet setIndex:index];

    NSMutableArray *copiedArray = [[self data] mutableCopyWithZone:zone];
    [newPlanet setData: copiedArray];
    [copiedArray release];

    return(newPlanet);
}

much appreciated

Gary

+3
source share
3 answers

There is nothing special here. Something like that:

NSMutableArray *copiedData = [[self data] mutableCopyWithZone:zone];
newPlanet.data = copiedData;
[copiedData release];
+6
source

You probably want

newPlanet->data = [data mutableCopyWithZone:zone];

, (, dot-access setData:), , .

+2

Have you tried something like [newPlanet setData:[[self data] mutableCopyWithZone:zone]];?

0
source

Source: https://habr.com/ru/post/1727539/


All Articles