1,模型model 拷贝。遵循NSCopying 协议
实现- (id)copyWithZone:(NSZone*)zone 方法
事例:
@interface LLJAdultEdu_basicInfor_list_Model : NSObject <NSCopying>
@property(nonatomic,strong)NSString *event_info_one_desc;
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *name_cn;
@end
@implementation LLJAdultEdu_basicInfor_list_Model
- (id)copyWithZone:(NSZone *)zone {
LLJAdultEdu_basicInfor_list_Model *model = [[self class] allocWithZone:zone];
model.event_info_one_desc = [_event_info_one_desc copy];
model.name = [_name copy];
model.name_cn = [_name_cn copy];
return model;
}
@end
2,数组包含数组,数组下的某一条数组的拷贝
//解析后台数据
LLJAdultEdu_basicInforModel * inforModel = [LLJAdultEdu_basicInforModel mj_objectWithKeyValues:obj[@"data"][@"list"]];
self.inforModel = inforModel;
@interface LLJAdultEdu_basicInforModel : NSObject
//配置model jl_dataArray 是数据key
@property(nonatomic,strong)NSMutableArray *jl_dataArray;
@end
@implementation LLJAdultEdu_basicInforModel
+(NSDictionary *)mj_objectClassInArray{
return @{@"jl_dataArray" : @"LLJAdultEdu_basicInfor_list_Model"};
}
- (NSMutableArray *)jl_dataArray{
if (!_jl_dataArray) {
_jl_dataArray = [[NSMutableArray alloc] init];
}
return _jl_dataArray;
}
@end
//单条数据模型 LLJAdultEdu_basicInfor_list_Model
@interface LLJAdultEdu_basicInfor_list_Model : NSObject
@property(nonatomic,strong)NSString *event_info_one_desc;
@property(nonatomic,strong)NSString *name;
@property(nonatomic,strong)NSString *name_cn;
@end
@implementation LLJAdultEdu_basicInfor_list_Model
- (id)copyWithZone:(NSZone *)zone {
LLJAdultEdu_basicInfor_list_Model *model = [[self class] allocWithZone:zone];
model.event_info_one_desc = [_event_info_one_desc copy];
model.name = [_name copy];
model.name_cn = [_name_cn copy];
return model;
}
@end
目的:
self.inforModel.jl_dataArray 数组下包含一个数组,单个数组下是一条一条LLJAdultEdu_basicInfor_list_Model
现在想在self.inforModel.jl_dataArray 第0个位置拷贝一个数据,添加到self.inforModel.jl_dataArray里面
遇到问题 :
如果直接取出第0个数据,然后拷贝,是不行的。如果输入框的内容改变,数据会错乱。猜测是因为只拷贝了数组,而没有拷贝数组里面单条的model
NSMutableArray * dataarray =self.inforModel.jl_dataArray[0];
[self.inforModel.jl_dataArray addObject:dataarray.copy];
解决办法:
NSMutableArray * dataarray = [[NSMutableArray alloc] init];
//取出第0个数据
dataarray =self.inforModel.jl_dataArray[0];
//新创建一个可变数组
NSMutableArray * copy_data = [[NSMutableArray alloc] init];
//遍历第0个数据
for (LLJAdultEdu_basicInfor_list_Model * jianli_model in dataarray) {
//jianli_model 模型拷贝,添加到新数组当中
[copy_dataaddObject:jianli_model.copy];
}
//新生成的数组添加到数据中
[self.inforModel.jl_dataArray addObject:copy_data];
网友评论