iOS--深浅拷贝深析

Apple官方文档如下:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Collections/Articles/Copying.html

一、博主最近被问到深浅拷贝的区别,想来想觉得理解只局限在表面,今天查查资料总结了一下,先来看几个词吧。

   浅 复 :在复制操作时,对于被复制的对象的每一层复制都是指针复制。

   深 复 :在复制操作时,对于被复制的对象至少有一层复制是对象复制。

   完全复制:在复制操作时,对于被复制的对象的每一层复制都是对象复制。


注:1在复制操作时,对于对象有n层是对象复制,我们可称作n级深复制,此处n应大于等于1

    2对于完全复制如何实现(目前通用的办法是:迭代法和归档),这里后续是否添加视情况而定

              暂时不做讲解。

    3、指针复制俗称指针拷贝,对象复制也俗称内容拷贝。


retain:始终是浅复制。引用计数每次加一。返回对象是否可变与被复制的对象保持一致。


copy:对于可变对象为深复制,引用计数不改变;对于不可变对象是浅复制,

         引用计数每次加一。始终返回一个不可变对象。


mutableCopy:始终是深复制,引用计数不改变。始终返回一个可变对象。


   不可变对象:值发生改变,其内存首地址随之改变。

   可变对象:无论值是否改变,其内存首地址都不随之改变。

   引用计数:为了让使用者清楚的知道,该对象有多少个拥有者(即有多少个指针指向同一内存地址)。


二、关于copy和mutableCopy的行为:不管是NSString这种元素类、还是NSArray这样的容器类、还是Mutable和非Mutable类,copy和mutableCopy调用后表现的行为到底是什么样完成取决于类本身NSCopying和NSMutableCopying协议是如何实现的。

想要正常调用copy和mutableCopy两个函数,那么类就一定要实现对应的协议。

1.      元素数据的copy和mutableCopy。

常用的NSString类,示例代码如下:

[cpp]  view plain copy
  1. NSString* string = @”a”;  
  2. NSString* stringCopy = [string copy];// stringCopy与string地址相同,retainCount+ 1   
  3. NSMutableString* stringMCopy = [string mutablecopy];// stringMCopy与string地址不同   
  4.    
  5. NSMutableString* stringM1 = [stringMCopy copy];//地址与stringMCopy不同,且为不可修改   
  6. NSMutableString* stringM2 = [stringMCopy mutablecopy];//地址与stringMCopy不同,可修改   
[cpp]  view plain copy
  1. NSString* string = @”a”;  
  2. NSString* stringCopy = [string copy];// stringCopy与string地址相同,retainCount+ 1  
  3. NSMutableString* stringMCopy = [string mutablecopy];// stringMCopy与string地址不同  
  4.    
  5. NSMutableString* stringM1 = [stringMCopy copy];//地址与stringMCopy不同,且为不可修改  
  6. NSMutableString* stringM2 = [stringMCopy mutablecopy];//地址与stringMCopy不同,可修改   

可以基本推出NSString和NSMutableString中两个协议的实现

 

[cpp]  view plain copy
  1. NSString:  
  2. - (id)copywithZone:(NSZone*)zone  
  3. {  
  4.   return self;  
  5. }  
  6.    
  7. - (id)mutableCopywithZone:(NSZone*)zone  
  8. {  
  9.   NSMutableString* copy =[[NSMutableString alloc] initxxxxxx];  
  10.   ....  
  11.   return copy;  
  12. }  
  13. NSMutableString:  
  14. - (id)copywithZone:(NSZone*)zone  
  15. {  
  16.   NSString* copy = [[NSStringalloc] initxxxxxx];  
  17.   ....  
  18.   return copy;//所以不可修改   
  19. }  
  20.    
  21. - (id)mutableCopywithZone:(NSZone*)zone  
  22. {  
  23.   NSMutableString* copy =[[NSMutableString alloc] initxxxxxx];  
  24.   ....  
  25.   return copy;  
  26. }  
[cpp]  view plain copy
  1. NSString:  
  2. - (id)copywithZone:(NSZone*)zone  
  3. {  
  4.   return self;  
  5. }  
  6.    
  7. - (id)mutableCopywithZone:(NSZone*)zone  
  8. {  
  9.   NSMutableString* copy =[[NSMutableString alloc] initxxxxxx];  
  10.   ....  
  11.   return copy;  
  12. }  
  13. NSMutableString:  
  14. - (id)copywithZone:(NSZone*)zone  
  15. {  
  16.   NSString* copy = [[NSStringalloc] initxxxxxx];  
  17.   ....  
  18.   return copy;//所以不可修改  
  19. }  
  20.    
  21. - (id)mutableCopywithZone:(NSZone*)zone  
  22. {  
  23.   NSMutableString* copy =[[NSMutableString alloc] initxxxxxx];  
  24.   ....  
  25.   return copy;  
  26. }  

2.      容器类的copy和mutableCopy。

常用类NSArray和NSMutableArray,看如下示例代码:

[cpp]  view plain copy
  1. Class1* obj1= ....;//正常初始化   
  2. NSArray* array = [[NSArray alloc] initWithObjects:obj1, nil];  
  3. NSArray* arrayCopy = [array copy];//地址不变,retaincount+1   
  4. NSMutableArray* arrayMCopy = [array mutableCopy];//地址改变,但是数组中成员指针和obj1相同,浅拷贝  
  5.   
  6. NSMutableArray* arrayM1 = [arrayMCopy Copy];//地址改变,但是数组中成员指针和obj1相同,浅拷贝。arrayM1为NSArray不可修改   
  7. NSMutableArray* arrayM2 = [arrayMCopy mutableCopy];//地址改变,但是数组中成员指针和obj1相同,浅拷贝  
[cpp]  view plain copy
  1. Class1* obj1= ....;//正常初始化  
  2. NSArray* array = [[NSArray alloc] initWithObjects:obj1, nil];  
  3. NSArray* arrayCopy = [array copy];//地址不变,retaincount+1  
  4. NSMutableArray* arrayMCopy = [array mutableCopy];//地址改变,但是数组中成员指针和obj1相同,浅拷贝  
  5.   
  6. NSMutableArray* arrayM1 = [arrayMCopy Copy];//地址改变,但是数组中成员指针和obj1相同,浅拷贝。arrayM1为NSArray不可修改  
  7. NSMutableArray* arrayM2 = [arrayMCopy mutableCopy];//地址改变,但是数组中成员指针和obj1相同,浅拷贝  
[cpp]  view plain copy
  1. //推断  
[cpp]  view plain copy
  1. //推断  
[cpp]  view plain copy
  1. NSArray:  
  2. - (id)copywithZone:(NSZone*)zone  
  3. {  
  4.   //伪码   
  5.   return [self retain];  
  6. }  
  7.   
  8. - (id)mutableCopywithZone:(NSZone*)zone  
  9. {  
  10.   NSMutableArray* copy = [[NSMutableString alloc] initxxxxxx];  
  11.   for (id element in self) {  
  12.     [copy addObject:element];//element retian count + 1   
  13.     ....  
  14.   }  
  15.   return copy;  
  16. }  
  17.   
  18. NSMutableArray:  
  19. - (id)copywithZone:(NSZone*)zone  
  20. {  
  21.   NSArray* copy = [[NSArray alloc] initXXX];  
  22.   /*把每个element加入到copy数组,retainCount+1*/  
  23.   ....  
  24.   return copy;  
  25. }  
  26.   
  27. - (id)mutableCopywithZone:(NSZone*)zone  
  28. {  
  29.   NSMutableArray* copy = [[NSMutableString alloc] initxxxxxx];  
  30.   for (id element in self) {  
  31.     [copy addObject:element];//element retian count + 1   
  32.     ....  
  33.   }  
  34.   return copy;  
  35. }  
[cpp]  view plain copy
  1. NSArray:  
  2. - (id)copywithZone:(NSZone*)zone  
  3. {  
  4.   //伪码  
  5.   return [self retain];  
  6. }  
  7.   
  8. - (id)mutableCopywithZone:(NSZone*)zone  
  9. {  
  10.   NSMutableArray* copy = [[NSMutableString alloc] initxxxxxx];  
  11.   for (id element in self) {  
  12.     [copy addObject:element];//element retian count + 1  
  13.     ....  
  14.   }  
  15.   return copy;  
  16. }  
  17.   
  18. NSMutableArray:  
  19. - (id)copywithZone:(NSZone*)zone  
  20. {  
  21.   NSArray* copy = [[NSArray alloc] initXXX];  
  22.   /*把每个element加入到copy数组,retainCount+1*/  
  23.   ....  
  24.   return copy;  
  25. }  
  26.   
  27. - (id)mutableCopywithZone:(NSZone*)zone  
  28. {  
  29.   NSMutableArray* copy = [[NSMutableString alloc] initxxxxxx];  
  30.   for (id element in self) {  
  31.     [copy addObject:element];//element retian count + 1  
  32.     ....  
  33.   }  
  34.   return copy;  
  35. }  

3.      深拷贝

上面提到的官方文档中介绍两种实现深拷贝的方法:

a.      用Array的initWithArray:  copyItems函数,如下:

NSArray *deepCopyArray=[[NSArray alloc] initWithArray: someArraycopyItems: YES];

调用后,会对原NSArray中的每个元素调用其copy函数,并把返回的id加入到新的数组中。所以这是依赖于Obj对象类实现的深拷贝,如果- (id)copywithZone:(NSZone*)zone是重新分配一块内存赋值后返回,那么就是真正的深拷贝。如果直接返回自身,那么它只是浅拷贝。

b.      用archiver方式:

NSArray* trueDeepCopyArray = [NSKeyedUnarchiverunarchiveObjectWithData: [NSKeyedArchiver archivedDataWithRootObject:oldArray]];

这是真正意义上的深拷贝,不依赖于实际类Copying协议的实现。

 

4. 用Category实现自定义的深拷贝deepmutableCopy,如:

[cpp]  view plain copy
  1. - (NSMutableArray *)mutableDeepCopy  
  2. {  
  3.     NSMutableArray *ret = [[NSMutableArrayalloc] initWithCapacity:[self count]];  
  4.     for (id value in self)  
  5.     {  
  6.         id oneCopy = nil;  
  7.         if ([value respondsToSelector:@selector(mutableDeepCopy)])  
  8.             oneCopy = [value mutableDeepCopy];  
  9.         else if ([value respondsToSelector:@selector(mutableCopy)])  
  10.             oneCopy = [value mutableCopy];  
  11.         if (oneCopy == nil)  
  12.             oneCopy = [value copy];  
  13.         [ret addObject: oneCopy];  
  14.     }  
  15.     return ret;  
  16. }  
[cpp]  view plain copy
  1. - (NSMutableArray *)mutableDeepCopy  
  2. {  
  3.     NSMutableArray *ret = [[NSMutableArrayalloc] initWithCapacity:[self count]];  
  4.     for (id value in self)  
  5.     {  
  6.         id oneCopy = nil;  
  7.         if ([value respondsToSelector:@selector(mutableDeepCopy)])  
  8.             oneCopy = [value mutableDeepCopy];  
  9.         else if ([value respondsToSelector:@selector(mutableCopy)])  
  10.             oneCopy = [value mutableCopy];  
  11.         if (oneCopy == nil)  
  12.             oneCopy = [value copy];  
  13.         [ret addObject: oneCopy];  
  14.     }  
  15.     return ret;  
  16. }  

你可能感兴趣的:(iOS--深浅拷贝深析)