{"id":5086,"date":"2014-03-30T18:44:12","date_gmt":"2014-03-30T18:44:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/deep-copying-nsmutablearray-issue-duplicate-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:44:12","modified_gmt":"2014-03-30T18:44:12","slug":"deep-copying-nsmutablearray-issue-duplicate-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/deep-copying-nsmutablearray-issue-duplicate-collection-of-common-programming-errors\/","title":{"rendered":"Deep Copying NSMutableArray Issue [duplicate]-Collection of common programming errors"},"content":{"rendered":"<blockquote>\n<p><strong>Possible Duplicate:<\/strong><br \/>\ndeep copy NSMutableArray in Objective-C ?<\/p>\n<\/blockquote>\n<p>I have two arrays of length 5 with items of a custom class I&#8217;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.<\/p>\n<p>1)<\/p>\n<pre><code>[[NSArray alloc] initWithArray:self.lifelineList copyItems:YES];\n<\/code><\/pre>\n<p>My copying protocol for this method is:<\/p>\n<pre><code>- (id) copyWithZone:(NSZone *)zone{\nLifeline *lifelineCopy = [[Lifeline allocWithZone:zone]init];\nlifelineCopy.name = name;\nlifelineCopy.phone = phone;\nlifelineCopy.email = email;\nlifelineCopy.isActive = isActive;\nlifelineCopy.contactID = contactID;\nreturn lifelineCopy;\n}\n<\/code><\/pre>\n<p>2)<\/p>\n<pre><code>[NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject: self.lifelineList]];  \n<\/code><\/pre>\n<p>My coding protocol for this method is:<\/p>\n<pre><code>- (id) initWithCoder:(NSCoder *)aDecoder{\nif (self=[super init]){\n    self.name = [aDecoder decodeObject];\n    self.phone = [aDecoder decodeObject];\n    self.email = [aDecoder decodeObject];\n    self.contactID = [aDecoder decodeIntegerForKey:@\"contactID\"];\n    self.isActive = [aDecoder decodeIntegerForKey:@\"isActive\"]&gt;0;\n}\nreturn self;\n}\n- (void) encodeWithCoder:(NSCoder *)aCoder{\n[aCoder encodeObject:self.name];\n[aCoder encodeObject:self.phone];\n[aCoder encodeObject:self.email];\n[aCoder encodeInteger:self.contactID forKey:@\"contactID\"];\n[aCoder encodeInteger:(NSInteger)self.isActive forKey:@\"isActive\"];\n}\n<\/code><\/pre>\n<p>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.<\/p>\n<p>I am happy to post more code if needed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Possible Duplicate: deep copy NSMutableArray in Objective-C ? I have two arrays of length 5 with items of a custom class I&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5086","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5086","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5086"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5086\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}