iOS学习笔记--浅拷贝和深拷贝——拷贝集合类(Copying Collections)

注:如果没有特别指出,这里的集合均指:collection。

对象的复制有两种:浅拷贝和深拷贝。正常的拷贝是浅拷贝,产生一个新的集合,共享原对象的所有权。深拷贝是从原对象创建新的对象,并把这些新对象添加到新的集合。这种差异如图1所示。

 

图 1. 浅拷贝和深拷贝

 

浅拷贝

有多种方法来创建一个集合(a collection)的浅拷贝。当你创建一个浅拷贝,便给原来集合的对象发送一个 retain 消息,对象的指针被复制到新的集合。清单1显示了一些使用浅拷贝来创建新的集合的方法。

清单 1 创建浅拷贝

? View Code OBJC
1
2
NSArray *shallowCopyArray=[someArray copyWithZone:nil];
NSDictionary *shallowCopyDict=[[NSDictionary alloc] initWithDictionary: someDictionary copyItems: NO];

这些技巧不局限于上述集合类。比如,您可以使用 copyWithZone: 方法拷贝一个集合(a set),或者使用 mutableCopyWithZone:,也可以使用 initWithArray:copyItems: 方法来拷贝数组。

 

深拷贝

有两种方法来创建一个集合的深拷贝。您可以使用集合的等价方法initWithArray:copyItems:,不过第2个参数为:YES。如果您使用这种方法创建了一个集合的深拷贝,集合中的每个对象都被发送一个 copyWithZone: 消息。如果集合中的对象都实现NSCopying 协议,对象被深拷贝到新的集合,这个集合是新复制的对象的唯一所有者。如果对象没有实现 NSCopying 协议,那么试图用这种方法复制它们会导致一个运行时错误结果。然而,copyWithZone: 产生一个浅拷贝。这种复制方法仅产生一个一级深度的拷贝。如果你只需要一级深度的拷贝,您可以使用清单2的方法。

清单 2 创建深拷贝

? View Code OBJC
1
NSArray *deepCopyArray=[[NSArray alloc] initWithArray: someArray copyItems: YES];

这个技巧也适用于其他集合。使用集合的等价方法 initWithArray:copyItems:,第2个参数为:YES

如果您需要一个真正的深拷贝,比如对于一个数组,可以使用archive和unchive来处理,数组元素要符合 NSCoding 协议。您可以使用清单3的方法。

清单 3 真正的深拷贝

? View Code OBJC
1
NSArray* trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData: [NSKeyedArchiver archivedDataWithRootObject: oldArray]];

 

举例说明

? View Code OBJC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    NSArray *n = [[NSArray alloc] initWithObjects:[NSMutableString stringWithString:@"123"],@"456", nil];
 
    // = [n copy]
    // The copy returned is immutable if the consideration “immutable vs. mutable” applies to the receiving object;
    // otherwise the exact nature of the copy is determined by the class.
    NSMutableArray *n1 = [n copyWithZone:nil];
 
    // = [n mutableCopy]
    // The copy returned is mutable whether the original is mutable or not.
    NSMutableArray *n2  = [n mutableCopyWithZone:nil];
 
    NSArray *n3 = [[NSArray alloc] initWithArray:n copyItems:NO];
 
    NSArray *n4 = [[NSArray alloc] initWithArray:n copyItems:YES];
 
    NSArray *n5 = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject: n]];
 
    NSLog(@"Array n = %@", n);
    NSLog(@"Array n1 = %@", n1);
    NSLog(@"Array n2 = %@", n2);
    NSLog(@"Array n3 = %@", n3);
    NSLog(@"Array n4 = %@", n4);
    NSLog(@"Array n5 = %@", n5);
 
    NSMutableString *nstr1 = [n objectAtIndex:0];
    [nstr1 appendString:@"abc"];
 
    [n2 addObject:@"xxx"];
 
    NSLog(@"Array n = %@", n);
    NSLog(@"Array n1 = %@", n1);
    NSLog(@"Array n2 = %@", n2);
    NSLog(@"Array n3 = %@", n3);
    NSLog(@"Array n4 = %@", n4);
    NSLog(@"Array n5 = %@", n5);

如图,左图是更改值之前的地址信息,右图是更改后的地址信息。

对第1种深拷贝方法来说,我们发现不可变元素,还是进行了指针复制(反正是不可变的,^_^)。

第2种深拷贝才是真正的深拷贝,全新的内存地址


转自:http://hxsdit.com/1477

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值