集合和非集合的copy与mutableCopy

  • 名词解释:

    非集合类:
    浅copy(指针地址copy):指针地址不同,内存地址相同;
    深copy(内存地址copy):指针地址不同,内存地址也不同;
    集合类:
    单层浅copy :集合对象浅copy ,集合内元素无copy;
    单层深copy :集合对象深copy ,集合内元素浅copy;

  • 简单结论

对‘非集合不可变’对象copy为浅copy(指针复制),其他为深copy;
对‘集合不可变对象’copy 为单层浅copy,其他均为单层深copy;


以下为详细解释,以及试验步骤:

1. 对非集合类对象的copy与mutableCopy

    NSString * string = @"string";    
    NSString * string1 = [string copy]; //
    NSString * string2 = [string mutableCopy];

    NSMutableString * mstring = [NSMutableString stringWithString:@"mutable"];
    NSString * string3 = [mstring copy];
    NSString * string4 = [mstring mutableCopy];
    
    NSLog(@"类名%@;内存地址:%p;指针地址%p\n",string.class,string,&string);
    NSLog(@"类名%@;内存地址:%p;指针地址%p\n",string1.class,string1,&string1);
    NSLog(@"类名%@;内存地址:%p;指针地址%p\n",string2.class,string2,&string2);

    NSLog(@"类名%@;内存地址:%p;指针地址%p\n",mstring.class,mstring,&mstring);
    NSLog(@"类名%@;内存地址:%p;指针地址%p\n",string3.class,string3,&string3);
    NSLog(@"类名%@;内存地址:%p;指针地址%p\n",string4.class,string4,&string4);
2020-07-09 20:47:36.351957+0800 PHTemplate[58589:2189532] string: 类名__NSCFConstantString;内存地址:0x10db41088;指针地址:0x7ffee20bf5d8

2020-07-09 20:47:36.352120+0800 PHTemplate[58589:2189532] string1: 类名__NSCFConstantString;内存地址:0x10db41088;指针地址:0x7ffee20bf5d0

2020-07-09 20:47:36.352245+0800 PHTemplate[58589:2189532] string2: 类名__NSCFString;内存地址:0x600003fac8d0;指针地址:0x7ffee20bf5c8

2020-07-09 20:47:36.352341+0800 PHTemplate[58589:2189532] mstring: 类名__NSCFString;内存地址:0x600003fac630;指针地址:0x7ffee20bf5c0

2020-07-09 20:47:36.352470+0800 PHTemplate[58589:2189532] string3: 类名NSTaggedPointerString;内存地址:0xa5548df62c4383c0;指针地址:0x7ffee20bf5b8

2020-07-09 20:47:36.352565+0800 PHTemplate[58589:2189532] string4: 类名__NSCFString;内存地址:0x600003fac660;指针地址:0x7ffee20bf5b0
  1. 由代码看出:

    只有string和string1的内存地址相同,指针地址不同;
    其他皆为,内存地址和指针地址都不同;

  2. 结合浅copy和深copy 的特性,由此可得结论:

    [immutableObject copy] // 浅复制。不可变
    [immutableObject mutableCopy] //深复制。可变
    [mutableObject copy] //深复制 不可变
    [mutableObject mutableCopy] //深复制 可变

2. 对集合类对象的copy与mutableCopy

    NSMutableString * string =  [NSMutableString stringWithString:@"hello"];
    NSArray * arr = @[string];
    NSArray * arr1 = [arr copy]; //
    NSArray * arr2 = [arr mutableCopy];
    
    NSMutableArray * mArr = [NSMutableArray arrayWithArray:@[string]];
    NSArray * arr3 = [mArr copy];
    NSArray * arr4 = [mArr mutableCopy];
    
    NSLog(@"arr:  类名%@;内存地址:%p;指针地址:%p---内存:%p;",arr.class,arr,&arr,arr.firstObject);
    NSLog(@"arr1: 类名%@;内存地址:%p;指针地址:%p---内存:%p;",arr1.class,arr1,&arr1,arr1.firstObject);
    NSLog(@"arr2: 类名%@;内存地址:%p;指针地址:%p---内存:%p;",arr2.class,arr2,&arr2,arr2.firstObject);
    
    NSLog(@"mArr: 类名%@;内存地址:%p;指针地址:%p---内存:%p;",mArr.class,mArr,&mArr,mArr.firstObject);
    NSLog(@"arr3: 类名%@;内存地址:%p;指针地址:%p---内存:%p;",arr3.class,arr3,&arr3,arr3.firstObject);
    NSLog(@"arr4: 类名%@;内存地址:%p;指针地址:%p---内存:%p;",arr4.class,arr4,&arr4,arr4.firstObject);
2020-07-09 21:12:03.297773+0800 PHTemplate[59848:2211493] arr: 类名__NSSingleObjectArrayI;内存:0x600001ce0200;指针:0x7ffee746f5b8---内存:0x6000010a5d40;
2020-07-09 21:12:03.297910+0800 PHTemplate[59848:2211493] arr1: 类名__NSSingleObjectArrayI;内存地址:0x600001ce0200;指针地址:0x7ffee746f5b0---内存:0x6000010a5d40;
2020-07-09 21:12:03.298026+0800 PHTemplate[59848:2211493] arr2: 类名__NSArrayM;内存地址:0x6000010a5f80;指针地址:0x7ffee746f5a8---内存:0x6000010a5d40;
2020-07-09 21:12:03.298134+0800 PHTemplate[59848:2211493] mArr: 类名__NSArrayM;内存地址:0x6000010a55f0;指针地址:0x7ffee746f5a0---内存:0x6000010a5d40;
2020-07-09 21:12:03.298244+0800 PHTemplate[59848:2211493] arr3: 类名__NSSingleObjectArrayI;内存地址:0x600001ce01f0;指针地址:0x7ffee746f598---内存:0x6000010a5d40;
2020-07-09 21:12:03.298333+0800 PHTemplate[59848:2211493] arr4: 类名__NSArrayM;内存地址:0x6000010a6010;指针地址:0x7ffee746f590---内存:0x6000010a5d40;
  1. 观察集合对象的指针和内存地址的变化
    结果:

    和非集合的类似

    • 只有arr和arr1的内存地址相同,指针地址不同;
    • 其他皆为,内存地址和指针地址都不同;
  2. 观察集合内的元素的指针和内存地址的变化
    结果:

    • 内存地址皆相同:可知没有对元素深copy
  3. 结论

    [immutableObject copy] //集合类:浅复制、 不可变 ; 元素:未copy
    [immutableObject mutableCopy] //集合类:深复制、 可变; 元素:未copy
    [mutableObject copy] //集合类:深复制、 不可变; 元素:未copy
    [mutableObject mutableCopy] //集合类:深复制、 可变; 元素:为copy

你可能感兴趣的:(集合和非集合的copy与mutableCopy)