zoukankan      html  css  js  c++  java
  • 终于碰到iOS对象集合深拷贝的坑

    从原始数组,拆分排列组合成新数组,同时给新的数组中的模型元素追加字段,数组的容量翻倍,如果不用深拷贝,后面追加的值就把前面的值覆盖了

    UnitModel *model1 = [UnitModel new];
    model1.studyPageId = @"1";
    
    UnitModel *model2 = [UnitModel new];
    model2.studyPageId = @"2";
    NSArray *modelArr = @[model1,model2];
    // 直接调用mutableCopy 并没真的赋值全新的模型数组,需要model遵守NSCopy,NSMutableCopying协议,实现copyWithZone 才能copy出真正的数组
    NSMutableArray <UnitModel *>*mutableModelArr = [modelArr mutableCopy];
    #pragma mark - 对象深复制,M1TestArrayHelper copy处理
    -(id)copyWithZone:(NSZone *)zone {
        ImageModel *model = [[self class] allocWithZone:zone];
        model.isResult = [_isResult copy];
        model.image = [_image copy];
        model.text = [_text copy];
        model.bgmArr = [_bgmArr copy];
        model.coverImageName = [_coverImageName copy];
        model.coverCount = [_coverCount copy];
        model.errorImageName = [_errorImageName copy];
        model.errorImageCount = [_errorImageName copy];
        return model;
    }
    
    -(id)mutableCopyWithZone:(NSZone *)zone {
        ImageModel *model = [[self class] allocWithZone:zone];
        model.isResult = [_isResult copy];
        model.image = [_image copy];
        model.text = [_text copy];
        model.bgmArr = [_bgmArr copy];
        model.coverImageName = [_coverImageName copy];
        model.coverCount = [_coverCount copy];
        model.errorImageName = [_errorImageName copy];
        model.errorImageCount = [_errorImageName copy];
        return model;
    }

    // 这里显示原始数据结构

    // 这里显示目标数据结构

  • 相关阅读:
    IDEA 如何批量修改变量名
    Idea 竖选文本、竖向选择、横向纵向选择文本代码
    IDEA中的.iml文件和.idea文件夹
    IDEA-Maven的Dependencies中出现红色波浪线
    接收来自路劲中的参数
    Jquery基础知识点
    JavaScript浏览器对象
    JavaScript面向对象编程
    HTML5 <iframe> 标签
    JavaScript标准对象
  • 原文地址:https://www.cnblogs.com/tufei7/p/9680039.html
Copyright © 2011-2022 走看看