首页 > 代码库 > 如何做到将可变数组进行实实在在的深拷贝--->容器类对象拷贝并且对象内部元素的拷贝

如何做到将可变数组进行实实在在的深拷贝--->容器类对象拷贝并且对象内部元素的拷贝

  首先说下我的需求:在A控制器,我有一个可变的自定义模型数组, 我要跳转到B控制器进行编辑的时候,我传入该模型数组.在B控制器我进行编辑,然后保存的时候把编辑后的数组逆传回去

 刚开始,没觉得哪里不对,后来发现在B控制器只要我改动了原来的数据,即使不点击保存,而是直接pop掉B控制器返回A控制器,我的模型数据还是会被改变,原因是我在编辑界面是直接拿着传入的模型数组对象进行编辑,所以,要想"保存"与"取消"分开,就不能直接拿着传入的模型数组对象进行编辑.  这个时候我们要做的是深拷贝一份数据出来 

 

  在Objective-C中并不是所有的类都支持拷贝;只有遵循NSCopying协议的类,才支持copy拷贝,只有遵循NSMutableCopying协议的类,才支持mutableCopy拷贝。如果没有遵循拷贝协议,拷贝时会出错

   如果我们想再我们自定义的类中支持copy和mutableCopy那么我们就需要使我们定义的类遵循NSCopying和NSMutableCopying协议,  然后再重写-(id) copyWithZone : (NSZone *) zone  和 -(id)mutableCopyWithZone : (NSZone *) zone

 

#import "SkuKeepingModel.h"

@interface SkuKeepingModel ()<NSCopying,NSMutableCopying>

@implementation SkuKeepingModel

- (id)copyWithZone:(NSZone *)zone
{
    SkuKeepingModel *newModel = [[self class] 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;
    
    return newModel;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
    SkuKeepingModel *newModel = [[self class] 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;
    
    return newModel;
}

在B控制器弄个 属性来拷贝传入的模型数组

@property (strong, nonatomic) NSMutableArray <SkuKeepingModel *> *tempSkuKeepingModels;

数组一传入B控制器马上进行拷贝  (是连容器内的元素也进行拷贝的哦)

SMutableArray *tempArr = [[NSMutableArray alloc] initWithArray:self.skuKeepingModels copyItems:YES];
self.tempSkuKeepingModels = tempArr;

然后,你在该控制器拿的是拷贝后的另外一个对象进行编辑了

 

   其实利用归档解档的操作也可以实现完全拷贝,

NSData *data =http://www.mamicode.com/ [NSKeyedArchiver archivedDataWithRootObject:self.skuKeepingModels];
NSMutableArray *newArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];

当然了,前提条件是你重写了这两个方法

- (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"];
.
.
.
.
} return self; } - (void) encodeWithCoder: (NSCoder *)coder { [coder encodeObject:self.keepingid forKey:@"keepingid"]; [coder encodeObject:self.productid forKey:@"productid"];
.
.
.
.
}

这里顺便说下,数据库返回的字段经常都会直接有 id 命名的   因为id是关键字 碰到这种情况我要进行key的替换  比如我们重命名为_id

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    if ([key isEqualToString:@"id"]) {
        self._id = value;
    }
}

 

如何做到将可变数组进行实实在在的深拷贝--->容器类对象拷贝并且对象内部元素的拷贝