1

Possible Duplicate:
deep copy NSMutableArray in Objective-C ?

I have two arrays of length 5 with items of a custom class I've built. I want to copy the first array into the second. Once copied I want these two arrays to be totally independent (I want to be able to change the first without affecting the second). I have tried several methods to no avail (when I make a change to one, the other is changed as well). They are shown below.

1)

[[NSArray alloc] initWithArray:self.lifelineList copyItems:YES];

My copying protocol for this method is:

- (id) copyWithZone:(NSZone *)zone{
Lifeline *lifelineCopy = [[Lifeline allocWithZone:zone]init];
lifelineCopy.name = name;
lifelineCopy.phone = phone;
lifelineCopy.email = email;
lifelineCopy.isActive = isActive;
lifelineCopy.contactID = contactID;
return lifelineCopy;
}

2)

[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject: self.lifelineList]];  

My coding protocol for this method is:

- (id) initWithCoder:(NSCoder *)aDecoder{
if (self=[super init]){
    self.name = [aDecoder decodeObject];
    self.phone = [aDecoder decodeObject];
    self.email = [aDecoder decodeObject];
    self.contactID = [aDecoder decodeIntegerForKey:@"contactID"];
    self.isActive = [aDecoder decodeIntegerForKey:@"isActive"]>0;
}
return self;
}
- (void) encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.name];
[aCoder encodeObject:self.phone];
[aCoder encodeObject:self.email];
[aCoder encodeInteger:self.contactID forKey:@"contactID"];
[aCoder encodeInteger:(NSInteger)self.isActive forKey:@"isActive"];
}

3) iterate through the first array, put the instance variables of each element into a temporary variable and then add that variable to the second array.

I am happy to post more code if needed.

Community
  • 1
  • 1
Silvae
  • 594
  • 1
  • 8
  • 20
  • This should help; in the past I've always used the arrayWithArray: call... http://stackoverflow.com/questions/3749657/nsmutablearray-arraywitharray-vs-initwitharray – Luke Jun 08 '11 at 18:22
  • @Krypton - tried it, no go. @Gilles - I created this question because the methods posted in that thread did not work for me – Silvae Jun 08 '11 at 18:41

1 Answers1

1
  1. Is your custom class properly implementing the NSCopying protocol? (I.e. implement copyWithZone: to return a completely independent instance.)

  2. Is you custom class properly implementing the NSCoding protocol to archive and decode all of the relevant instance variables?

  3. This won't work unless you actually copy the objects themselves. Simply assigning to a temporary variable does nothing at runtime.

Daniel Dickison
  • 21,832
  • 13
  • 69
  • 89
  • I have edited the question to show my copy/coding protocols...Perhaps I am doing them wrong? – Silvae Jun 08 '11 at 18:28
  • Assuming `name`, `email`, and `phone` are `NSString` declared as `copy` properties, I think your copy/coding methods are correct. There must be something else going on. How exactly are you determining that the deep copy is not working? – Daniel Dickison Jun 08 '11 at 18:59
  • even though the question was closed I'd still like to thank you for your help. I found the issue (turns out it was unrelated to the actual copying process). – Silvae Jun 08 '11 at 19:45