今天敲代码的时候,两个数组中放入同一套model数据,发现model的指针是一样的,操作其中一个数组的数据会导致另外一个数组的数据也跟着改变,所以必须给model数据做深拷贝
通过copy深拷贝操作后如下:
那么如何实现model的copy方法呢?
首先遵循<NSCopying, NSMutableCopying>
协议
.m中实现方法
- (id)copyWithZone:(NSZone *)zone{
ProductTagModel * model = [[ProductTagModel allocWithZone:zone] init];
model.label_id = self.label_id;//self是被copy的对象
model.label_img = self.label_img;
model.label_name = self.label_name;
model.isSelected = self.isSelected;
return model;
}
- (id)mutableCopyWithZone:(NSZone *)zone{
ProductTagModel * model = [[ProductTagModel allocWithZone:zone] init];
model.label_id = self.label_id;//self是被copy的对象
model.label_img = self.label_img;
model.label_name = self.label_name;
model.isSelected = self.isSelected;
return model;
}