[Objective-C] Copy 和 MutableCopy

看了几篇文章,因为文章很新手向,所以内容很繁琐。故整理一下重点,写了测试程序去了解几个知识点,不讨论基本概念。新博客wossoneri.com传送门

非集合类对象的copy与mutableCopy

      //NSString & NSMutableString
      NSString *strA = @"origin";
      NSLog(@"strA        pointer: %p content: %@", strA, strA);
      
      NSString *strACopy = [strA copy];
      NSLog(@"strCopy     pointer: %p content: %@", strACopy, strACopy);
      
      NSMutableString *strAMCopy = [strA mutableCopy];
      NSLog(@"strMCopy    pointer: %p content: %@", strAMCopy, strAMCopy);
      
      
      NSMutableString *strB = [NSMutableString stringWithString:@"mutable string"];
      NSLog(@"strB        pointer: %p content: %@", strB, strB);
      
      NSMutableString *strBCopy = [strB copy];
      NSLog(@"strBCopy    pointer: %p content: %@", strBCopy, strBCopy);
      
      NSMutableString *strBMCopy = [strB mutableCopy];
      NSLog(@"strBMCopy   pointer: %p content: %@", strBMCopy, strBMCopy);

//输出
strA        pointer: 0x100003180 content: origin
strCopy     pointer: 0x100003180 content: origin
strMCopy    pointer: 0x100102e90 content: origin
strB        pointer: 0x100700510 content: mutable string
strBCopy    pointer: 0x100700570 content: mutable string
strBMCopy   pointer: 0x1006002c0 content: mutable string

在非集合类对象中:对immutable对象进行copy操作,是指针复制,mutableCopy操作时内容复制;对mutable对象进行copymutableCopy都是内容复制。

结论

  • [immutableObject copy] // 浅复制
  • [immutableObject mutableCopy] //深复制
  • [mutableObject copy] //深复制
  • [mutableObject mutableCopy] //深复制

集合类对象的copy与mutableCopy

      NSArray *arrayA = @[@[@"A", @"B"], @[@"C", @"D"]];
      NSLog(@"arrayA      pointer:%p  contentA:%p contentB:%p", arrayA, arrayA[0], arrayA[1]);
      
      NSArray *arrCopyA = [arrayA copy];
      NSLog(@"arrCopyA    pointer:%p  contentA:%p contentB:%p", arrCopyA, arrCopyA[0], arrCopyA[1]);
      
      NSMutableArray *arrMCopyA = [arrayA mutableCopy];
      NSLog(@"arrMCopyA   pointer:%p  contentA:%p contentB:%p", arrMCopyA,arrMCopyA[0], arrMCopyA[1]);

      
      NSMutableArray *arrayB = [NSMutableArray arrayWithObjects:@[@"A", @"B"], @[@"C", @"D"], nil];
      NSLog(@"arrayB      pointer:%p  contentA:%p contentB:%p", arrayB, arrayB[0], arrayB[1]);
      
      NSArray *arrCopyB = [arrayB copy];
      NSLog(@"arrCopyB    pointer:%p  contentA:%p contentB:%p", arrCopyB, arrCopyB[0], arrCopyB[1]);
      
      NSMutableArray *arrMCopyB = [arrayB mutableCopy];
      NSLog(@"arrMCopyB   pointer:%p  contentA:%p contentB:%p", arrMCopyB,arrMCopyB[0], arrMCopyB[1]);
      
//输出
arrayA      pointer:0x100700860  contentA:0x100700840 contentB:0x1007007c0
arrCopyA    pointer:0x100700860  contentA:0x100700840 contentB:0x1007007c0
arrMCopyA   pointer:0x100700990  contentA:0x100700840 contentB:0x1007007c0

arrayB      pointer:0x100701970  contentA:0x100700880 contentB:0x100701730
arrCopyB    pointer:0x100102080  contentA:0x100700880 contentB:0x100701730
arrMCopyB   pointer:0x100701ab0  contentA:0x100700880 contentB:0x100701730

可以看到拷贝过程和非集合类大体相同。注意一点,就是拷贝过程只是针对对象本身,而对象内部的元素还是指针复制,从上面打印的输出就可以看出来。

示例

看如下代码,有一个mutable数组,赋值给immutable数组

@property (nonatomic, strong) NSArray *array;
  NSMutableArray *mArray = [NSMutableArray array];
  [mArray addObject:@"AA"];
  _array = mArray;
  NSLog(@"mArray: %p, _array: %p", mArray, _array);
  [mArray addObject:@"BB"];
  NSLog(@"mArray = %@, _array = %@", mArray, _array);

其中,直接对array属性赋值,这样,_arraymArray便是指向同一对象的指针,因为NSMutableArrayNSArray的子类,故_array能够接受mArray的值。

输出结果:

mArray: 0x126ec3e00, _array: 0x126ec3e00
mArray = (
    AA,
    BB
), _array = (
    AA,
    BB
)

下面做个修改,把

_array = mArray;

改为

_array = [mArray copy];

想想结果如何?

mArraymutable对象,拷贝属于(不完全)深拷贝,所以可以想到_array的指针和mArray并不相同,所以后面mArray改变内容也与_array无关。

输出:

mArray: 0x154dc8070, _array: 0x154dcf4e0
mArray = (
    AA,
    BB
), _array = (
    AA
)

小结

Reference
小结 iOS 中的 copy
iOS 集合的深复制与浅复制

posted @ 2016-06-04 23:40  Wossoneri  阅读(485)  评论(0编辑  收藏  举报