How to make a real deep copy of a variable group---> Copy of a container class object and a copy of an object's internal elements

Source: Internet
Author: User

First of all, I need to say: In a controller, I have a mutable custom model array, I want to jump to the B controller for editing, I pass in the model array. In the B controller I make edits, and then save the edited array back to the reverse pass

At first, did not think what was wrong, and later found in the B controller as long as I changed the original data, even if not click Save, but directly pop off the B controller returned a controller, my model data will be changed, because I am in the editing interface is directly with the incoming model array object for editing, so, to "save"  Apart from "Cancel", you cannot directly take the incoming model array object for editing. What we're going to do at this point is to make a deep copy of the data.

In Objective-c, not all classes support copies, and only those that follow the Nscopying protocol support copy copies, and only those that follow the Nsmutablecopying protocol support mutablecopy copies. If the copy protocol is not followed, a copy error occurs

If we want to support copy and Mutablecopy in our custom classes then we need to make our defined classes follow the nscopying and nsmutablecopying protocols, and then rewrite-(ID) Copywithzone: (Nszone *) Zone and-(ID) Mutablecopywithzone: (Nszone *) zone

#import " SkuKeepingModel.h " @interface Skukeepingmodel () <NSCopying,NSMutableCopying>

@implementation Skukeepingmodel

- (ID) Copywithzone: (Nszone *) zone{Skukeepingmodel*newmodel = [Selfclass] Allocwithzone:zone]; Newmodel.keepingid=_keepingid; Newmodel.productid=_productid; Newmodel.reftable=_reftable; Newmodel.refrecordid=_refrecordid; Newmodel.keepingname=_keepingname; Newmodel.portrait=_portrait; Newmodel.quantity=_quantity; Newmodel.marketprice=_marketprice; Newmodel.discountprice=_discountprice; Newmodel.uunitprice=_uunitprice; Newmodel.soldcount=_soldcount; Newmodel.recordstatus=_recordstatus; Newmodel.createdate=_createdate; Newmodel.modifydate=_modifydate; returnNewmodel;}- (ID) Mutablecopywithzone: (Nszone *) zone{Skukeepingmodel*newmodel = [Selfclass] Allocwithzone:zone]; Newmodel.keepingid=_keepingid; Newmodel.productid=_productid; Newmodel.reftable=_reftable; Newmodel.refrecordid=_refrecordid; Newmodel.keepingname=_keepingname; Newmodel.portrait=_portrait; Newmodel.quantity=_quantity; Newmodel.marketprice=_marketprice; Newmodel.discountprice=_discountprice; Newmodel.uunitprice=_uunitprice; Newmodel.soldcount=_soldcount; Newmodel.recordstatus=_recordstatus; Newmodel.createdate=_createdate; Newmodel.modifydate=_modifydate; returnNewmodel;}

Get a property on the B controller to copy the incoming model array

@property (Strong, nonatomic) Nsmutablearray <skukeepingmodel *> *tempskukeepingmodels;

An array of incoming B controllers are immediately copied (the elements inside the container are also copied)

Smutablearray *temparr == Temparr;

Then you edit the other object in the controller with the copy.

In fact, the use of archive files can also achieve a full copy of the operation,

NSData *data =*newarray = [Nskeyedunarchiver unarchiveobjectwithdata:data];

Of course, the precondition is that you rewrite the two methods.

-(ID) Initwithcoder: (Nscoder *) Coder-(void) Encodewithcoder: (Nscoder *) coder

- (ID) Initwithcoder: (Nscoder *) coder{if(self =[Super Init]) {Self.keepingid= [Coder Decodeobjectforkey:@"Keepingid"]; Self.productid= [Coder Decodeobjectforkey:@ "ProductID"];
.
.
.
. } returnSelf ;}- (void) Encodewithcoder: (Nscoder *) coder{[Coder EncodeObject:self.keepingid Forkey:@ "Keepingid"]; [Coder EncodeObject:self.productid Forkey:@ "ProductID"];
.
.
.
.}

By the way, the fields returned by the database will often be named after the ID because the ID is the keyword. I'm going to replace the key, like we renamed _ID.

-(void) SetValue: (ID) value forundefinedkey: (NSString *) key{    if ([key Isequaltostring:@ "ID"]) {        = value;    }}

How to make a real deep copy of a variable group---> Copy of a container class object and a copy of an object's internal elements

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.