IOS深浅拷贝的深入分析

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

最近用Core Data做数据管理,Fetch到NSArray类型的结果数组,为了能够进行增、删、改,我把它MutableCopy到一个NSMutableArray数组中。增加、删除用manageModelsContext中的接口操作即可,修改则把NSArray结果中需要修改的实例取出来进行更改,然后用manageModelsContext保存即可完成修改到sqlite文件。

可是问题是我发现用MutableCopy得到的数组中取出的元素直接修改后保存,也能完成修改操作。于是就产生了这样的一个疑问:MutableCopy是浅拷贝??再下来我就发现copy相关的东西我还完全没有搞清楚。

 

首先关于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不同,可修改   

可以基本推出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. }  

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. //推断  
[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. }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值