首页 > 代码库 > Objective-C中的深拷贝和浅拷贝
Objective-C中的深拷贝和浅拷贝
在Objective-C中对象之间的拷贝分为浅拷贝和深拷贝。说白了,浅拷贝就是拷贝对象的地址,对象里面存的内容仍然是一份,没有新的内存被分配。深拷贝就是重写分配一块内存,然后把另一个对象的内容原封不动的给我拿过来。所以看对象是否为深拷贝还是浅拷贝就得看对象的内存地址,因为OC中用引用计数的方式来进行内存管理的所以我们也可以通过观察对象retainCount的变化来分析对象之间是否是深拷贝还是浅拷贝。下面会通过对不同类型的对象进行测试来详细的理解一下对象的深拷贝和浅拷贝。
那么对象大体都分为哪些类型呢?从可变不可变和容器类非容器类的角度可以把对象分为一下几种,那么什么是容器类呢?容器类就是用该类声明的对象可以去容纳其他对象,非容器类则没有这些功能。那么什么是可变或者不可变的呢?可变的时内存的大小是可以根据需要改变,而不可变的就是分配完以后就不可以改变他的内存空间(以上是本人的理解,不足或理解偏颇之处还请批评指正,转载本文请注明出处)
1. 非容器不可变对象,比如NSString
2.非容器可变对象:比如NSMutableString
3.容器类不可变对象: 比如NSArray
4.容器类可变对象: 比如NSMutableArray
在观察深浅拷贝之前先得了解一下retain,copy和mutableCopy的特点,特点如下:
1.retain:始终是浅复制。引用计数每次加一。返回对象是否可变与被复制的对象保持一致。
2.copy:对于可变对象为深复制,引用计数不改变;对于不可变对象是浅复制, 引用计数每次加一。始终返回一个不可变对象。
3.mutableCopy:始终是深复制,引用计数不改变。始终返回一个可变对象。
下面把容器非容器,可变非可变结合着retain,copy,mutableCopy来仔细的分析一下OC中得深浅拷贝,代码走起:
1.非容器 + 不可变对象 + retain + copy + mutableCopy
代码说明:先定义一个非容器类不可变对象,然后同过retain,copy和mutableCopy的方式把值copy给一个非容器类不可变对象,最后把各个对象的地址输出,用NSString来做测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | NSLog(@ "非容器类不可变对象拷贝NSString" ); NSString *str = @"ludashi"; NSLog(@ " str = %@, init_str.retainCount = %d" , str, ( int )str.retainCount); //把str通过retain方式把值赋给str1 NSString *str1 = [str retain]; NSLog(@ "str1 = %@, retain_str1.retainCount = %d" ,str1, ( int )str1.retainCount); //把str通过copy的方式把值赋给str2 NSString *str2 = [str copy]; NSLog(@ "str2 = %@, copy_str2.retainCount = %d" , str2, ( int )str2.retainCount); //把str通过mutableCopy的方式把值赋给str3 NSString *str3 = [str mutableCopy]; NSLog(@ "str3 = %@, mutableCopy_str3.retainCount = %d" , str3, ( int )str3.retainCount); //分别输出每个字符串的内存地址 NSLog(@ " str-p = %p" , str); NSLog(@ "str1-p = %p" , str1); NSLog(@ "str2-p = %p" , str2); NSLog(@ "str3-p = %p" , str3); |
代码运行结果:
1 2 3 4 5 6 7 8 9 | 2014-08-06 10:49:04.422 Memory[761:303] 非容器类不可变对象拷贝NSString 2014-08-06 10:49:04.423 Memory[761:303] str = ludashi, init_str.retainCount = -1 2014-08-06 10:49:04.424 Memory[761:303] str1 = ludashi, retain_str1.retainCount = -1 2014-08-06 10:49:04.424 Memory[761:303] str2 = ludashi, copy_str2.retainCount = -1 2014-08-06 10:49:04.424 Memory[761:303] str3 = ludashi, mutableCopy_str3.retainCount = 1 2014-08-06 10:49:04.425 Memory[761:303] str-p = 0x100004170 2014-08-06 10:49:04.425 Memory[761:303] str1-p = 0x100004170 2014-08-06 10:49:04.425 Memory[761:303] str2-p = 0x100004170 2014-08-06 10:49:04.426 Memory[761:303] str3-p = 0x100107790 |
代码运行结果分析:
1. 对于非容器类的不可变对象retain和copy为浅拷贝,mutableCopy为深拷贝
2. 浅拷贝获得的对象的地址和原有对象的地址一致
3.而深拷贝返回新的内存地址,并且返回的对象为可变对象
2.非容器 + 可变对象 + retain + copy + mutableCopy
接下来我们来测试下非容器类的可变对象的深浅拷贝
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | NSLog(@ "非容器类的可变对象拷贝" ); NSMutableString *s = [NSMutableString stringWithFormat:@ "ludashi" ]; NSLog(@ " s = %@, init_s_retainCount = %d" , s, ( int )s.retainCount); //把s通过retain的方式把值 赋给s1; NSMutableString *s1 = [s retain]; NSLog(@ "s1 = %@, retain_s1_retainCount = %d" , s1, ( int )s1.retainCount); //把s通过copy的方式把值赋给s2; NSMutableString *s2 = [s copy]; NSLog(@ "s2 = %@, copy_s2_retianCount = %d" , s2, ( int )s2.retainCount); //把s通过mutableCopy的方式把值赋给s3 NSMutableString *s3 = [s mutableCopy]; NSLog(@ "s3 = %@, mutable_s3_retainCount = %d" , s3, ( int )s3.retainCount); //打印每个非容器类可变对象的地址 NSLog(@ " s_p = %p" , s); NSLog(@ "s1_p = %p" , s1); NSLog(@ "s2_p = %p" , s2); NSLog(@ "s3_p = %p" , s3); |
运行结果如下:
1 2 3 4 5 6 7 8 9 | 2014-08-06 10:49:04.429 Memory[761:303] 非容器类的可变对象拷贝 2014-08-06 10:49:04.430 Memory[761:303] s = ludashi, init_s_retainCount = 1 2014-08-06 10:49:04.430 Memory[761:303] s1 = ludashi, retain_s1_retainCount = 2 2014-08-06 10:49:04.430 Memory[761:303] s2 = ludashi, copy_s2_retianCount = 1 2014-08-06 10:49:04.431 Memory[761:303] s3 = ludashi, mutable_s3_retainCount = 1 2014-08-06 10:49:04.431 Memory[761:303] s_p = 0x100107860 2014-08-06 10:49:04.431 Memory[761:303] s1_p = 0x100107860 2014-08-06 10:49:04.432 Memory[761:303] s2_p = 0x100206470 2014-08-06 10:49:04.432 Memory[761:303] s3_p = 0x1003004a0 |
运行结果分析:
1.retian对对可变对象为浅拷贝
2.copy对可变对象非容器类为深拷贝
3.mutableCopy对可变非容器类为深拷贝
3.容器类 + 非可变对象 + retain + copy + mutableCopy
下面对容器类的非可变对象进行测试
? ? ?代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | NSLog(@ "容器类不可变对象拷贝" ); NSArray *array = [NSArray arrayWithObjects:@ "a" , @ "b" , nil]; NSLog(@ " array[0] = %@, init_array.retainCount = %d" , array[0], ( int )array.retainCount); //把array通过retain方式把值赋给array1 NSArray *array1 = [array retain]; NSLog(@ "array1[0] = %@, retain_array1.retainCount = %d" , array1[0], ( int )array1.retainCount); //把array通过copy的方式把值赋给array2 NSArray *array2 = [array copy]; NSLog(@ "array2[0] = %@, copy_array.retainCount = %d" , array2[0], ( int )array2.retainCount); //把array通过mutableCopy方式把值赋给array3 NSArray *array3 = [array mutableCopy]; NSLog(@ "array3[0] = %@, mutableCopy_array3.retainCount = %d" , array3[0], ( int )array3.retainCount); //分别输出每个地址 NSLog(@ " array_p = %p" , array); NSLog(@ "array1_p = %p" , array1); NSLog(@ "array2_p = %p" , array2); NSLog(@ "array3_p = %p" , array3); |
? ? ?运行结果:
1 2 3 4 5 6 7 8 9 | 2014-08-06 10:49:04.426 Memory[761:303] 容器类不可变对象拷贝 2014-08-06 10:49:04.427 Memory[761:303] array[0] = a, init_array.retainCount = 1 2014-08-06 10:49:04.427 Memory[761:303] array1[0] = a, retain_array1.retainCount = 2 2014-08-06 10:49:04.427 Memory[761:303] array2[0] = a, copy_array.retainCount = 3 2014-08-06 10:49:04.428 Memory[761:303] array3[0] = a, mutableCopy_array3.retainCount = 1 2014-08-06 10:49:04.428 Memory[761:303] array_p = 0x100400660 2014-08-06 10:49:04.428 Memory[761:303] array1_p = 0x100400660 2014-08-06 10:49:04.428 Memory[761:303] array2_p = 0x100400660 2014-08-06 10:49:04.429 Memory[761:303] array3_p = 0x100206700 |
4.容器类 + 可变对象 + retain + copy + mutableCopy
下面对容器类的可变对象进行测试
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | NSLog(@ "容器类的可变对象的拷贝" ); NSMutableArray *m_array = [NSMutableArray arrayWithObjects:@ "ludashi" , nil]; NSLog(@ " m_array[0] = %@, init_m_array_retainCount = %d" , m_array[0], ( int )m_array.retainCount); //把m_array通过retain把值赋给m_array1 NSMutableArray *m_array1 = [m_array retain]; NSLog(@ "m_array1[0] = %@, retain_m_array1_retainCount = %d" , m_array1[0], ( int )m_array1.retainCount); //把m_array通过copy把值赋给m_array2 NSMutableArray *m_array2 = [m_array copy]; NSLog(@ "m_array2[0] = %@, copy_m_array2_retainCount = %d" , m_array2[0], ( int )m_array2.retainCount); //把m_array通过mytableCopy把值赋给m_array3 NSMutableArray *m_array3 = [m_array mutableCopy]; NSLog(@ "m_array3[0] = %@, mutable_m_array3_retainCount = %d" , m_array3[0], ( int )m_array3.retainCount ); //打印输出每个可变容器对象的地址 NSLog(@ " m_array_p = %p" , m_array); NSLog(@ "m_array_p1 = %p" , m_array1); NSLog(@ "m_array_p2 = %p" , m_array2); NSLog(@ "m_array_p3 = %p" , m_array3); |
? ?运行结果:
1 2 3 4 5 6 7 8 9 | 2014-08-06 10:49:04.433 Memory[761:303] 容器类的可变对象的拷贝 2014-08-06 10:49:04.433 Memory[761:303] m_array[0] = ludashi, init_m_array_retainCount = 1 2014-08-06 10:49:04.433 Memory[761:303] m_array1[0] = ludashi, retain_m_array1_retainCount = 2 2014-08-06 10:49:04.434 Memory[761:303] m_array2[0] = ludashi, copy_m_array2_retainCount = 1 2014-08-06 10:49:04.434 Memory[761:303] m_array3[0] = ludashi, mutable_m_array3_retainCount = 1 2014-08-06 10:49:04.434 Memory[761:303] m_array_p = 0x100206550 2014-08-06 10:49:04.435 Memory[761:303] m_array_p1 = 0x100206550 2014-08-06 10:49:04.435 Memory[761:303] m_array_p2 = 0x1002054d0 2014-08-06 10:49:04.435 Memory[761:303] m_array_p3 = 0x100300420 |
? ?上面的代码以及代码的运行结果翻来复去就是在验证下面的结论:
? 1.retain:始终是浅复制。引用计数每次加一。返回对象是否可变与被复制的对象保持一致。
2.copy:对于可变对象为深复制,引用计数不改变;对于不可变对象是浅复制, 引用计数每次加一。始终返回一个不可变对象。
3.mutableCopy:始终是深复制,引用计数不改变。始终返回一个可变对象。
自定义类对象之间的深浅拷贝问题
在Objective-C中并不是所有的类都支持拷贝;只有遵循NSCopying协议的类,才支持copy拷贝,只有遵循NSMutableCopying协议的类,才支持mutableCopy拷贝。如果没有遵循拷贝协议,拷贝时会出错。
如果我们想再我们自定义的类中支持copy和mutableCopy那么我们就需要使我们定义的类遵循NSCopying和NSMutableCopying协议,代码如下:
1 @interface Test : NSObject <NSCopying, NSMutableCopying>
然后再重写-(id) copyWithZone : (NSZone *) zone 和 -(id)mutableCopyWithZone : (NSZone *) zone
重写-(id) copyWithZone :(NSZone *)zone方法如下
//浅拷贝-(id) copyWithZone : (NSZone *) zone{ return [self retain]; }//深拷贝-(id) mutableCopyWithZone : (NSZone *) zone{ Test *test = [[Test allocWithZone : zone] init]; test.property = self.property; return test; }