copy、mutableCopy与浅拷贝、深拷贝之间的联系

2016-06-24  本文已影响0人  Gary_fei

浅拷贝与深拷贝de区别

实例

  1. 非容器类前后的变化(NSString、NSNumber等)
  //不可变对象 copy
    NSString *str1       = @"123";
    NSString *copyStr1   = [str1 copy];
    NSLog(@"str1     = %p",str1);
    NSLog(@"copyStr1 = %p",copyStr1);   
//输出内容(地址相同)
    str1      = 0x100080068
    copyStr1  = 0x100080068

  //不可变对象 mutableCopy
    NSString *str2       = [NSMutableString stringWithString:@"123"];
    NSString *copyStr2   = [str2 mutableCopy];
    NSLog(@"str2     = %p",str2);
    NSLog(@"copyStr2 = %p",copyStr2);
//输出结果 (地址不相同)
    str2     = 0x146675670
    copyStr2 = 0x146675c80
// 可变对象 copy
  NSMutableString *str3           = [NSMutableString stringWithString:@"123"];
  NSMutableString *mutableStr3    = [str3 copy];
  NSLog(@"str3        = %p",str3);
  NSLog(@"mutableStr3 = %p",mutableStr3);
//输出结果(地址不相同)
  str3        = 0x146676cc0
  mutableStr3 = 0xa000000003332313 

  // 可变对象 mutableCopy
  NSMutableString *str4           = [NSMutableString stringWithString:@"123"];
  NSMutableString *mutableStr4    = [str3 mutableCopy];
  NSLog(@"str4        = %p",str4);
  NSLog(@"mutableStr4 = %p",mutableStr4);
//输出结果(地址不相同)
  str4        = 0x146618740
  mutableStr4 = 0x1466755a0
  1. 容器类前后地址变化
// 不可变对象 copy
    NSArray *array1     = @[@"111"];
    NSArray *copyArray1 = [array1 copy];
    NSLog(@"array1          = %p",array1);
    NSLog(@"copyArray1      = %p",copyArray1);
    NSLog(@"array1[0]       = %@",array1[0]);
    NSLog(@"copyArray1[0]   = %@",copyArray1[0]);
//输出结果(地址相同,内容相同)
    array1          = 0x145e075d0
    copyArray1      = 0x145e075d0
    array1[0]       = 111
    copyArray1[0]   = 111

    // 不可变对象 mutableCopy
     NSArray *array2         = @[@"111"];
     NSArray *mutableArray2  = [array2 mutableCopy];
     NSLog(@"array2          = %p",array2);
     NSLog(@"mutableArray2   = %p",mutableArray2);
     NSLog(@"array2[0]       = %@",array2[0]);
     NSLog(@"copyArray2[0]   = %@",mutableArray2[0]);
//输出结果(地址不相同, 内容相同)
     array2             = 0x145e23010
     mutableArray2      = 0x145e115f0
     array2[0]          = 111
     mutableArray2[0]   = 111
// 可变对象 copy
    NSMutableArray *array3      = [NSMutableArray arrayWithArray:@[@"111"]];
    NSMutableArray *copyArray3  = [array3 copy];
    NSLog(@"array3          = %p",array3);
    NSLog(@"copyArray3      = %p",copyArray3);
    NSLog(@"array3[0]       = %@",array3[0]);
    NSLog(@"copyArray3[0]   = %@",copyArray3[0]);
   //输出结果(地址不相同, 内容不相同)
    array3          = 0x145e0c950
    copyArray3      = 0x145e23030
    array3[0]       = 111
    copyArray3[0]   = 111

    // 可变对象 mutableCopy
    NSMutableArray *array4          = [NSMutableArray arrayWithArray:@[@"111"]];
    NSMutableArray *mutableArray4   = [array4 mutableCopy];
    NSLog(@"array4          = %p",array4);
    NSLog(@"mutableArray4   = %p",mutableArray4);
    NSLog(@"array4[0]       = %@",array4[0]);
    NSLog(@"mutableArray4[0]= %@",mutableArray4[0]);
//输出结果(地址不相同, 内容相同)
    array4          = 0x145e02010
    mutableArray4   = 0x145e22670
    array4[0]       = 111
    mutableArray4[0]= 111

总结:

  1. 无论是非容器类对象或容器类对象都符合以下规则:
- 对于不可变对象,copy是浅拷贝,mutableCopy是深拷贝
- 对于可变对象,copy和mutableCopy都是深拷贝
- 不论是可变还是不可变对像,copy返回的对象都是不可变的
  1. 对于容器类对象
    不论是可变的还是不可变的,copy和mutableCopy返回的容器对象里所包含的对象的地址和之前的都是一样的,即容器内对象都是浅拷贝

参考资料:耳东米青

上一篇下一篇

猜你喜欢

热点阅读