Deep NSArray Mutable Copy Category behaviour (nsarray 深度拷贝)
来源:互联网 发布:刺客信条枭雄a卡优化 编辑:程序博客网 时间:2024/06/10 23:12
Deep NSArray Mutable Copy Category behaviour@implementation NSArray (DeepCopy)- (id)deepMutableCopy { NSMutableArray *mutableCopy = (NSMutableArray *)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFArrayRef)self, kCFPropertyListMutableContainers); return mutableCopy;}@end@implementation NSDictionary (DeepCopy)- (id)deepMutableCopy { NSMutableDictionary *mutableCopy = (NSMutableDictionary *)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFDictionaryRef)self, kCFPropertyListMutableContainers); return mutableCopy;}@endNSMutableArray *copiedArray = [resultsArray deepMutableCopy];
以上未起作用~ , 查看apple官方文档解释如下:
There are two ways to make deep copies of a collection. You can use the collection’s equivalent ofinitWithArray:copyItems:
with YES
as the second parameter. If you create a deep copy of a collection in this way, each object in the collection is sent acopyWithZone:
message. If the objects in the collection have adopted theNSCopying
protocol, the objects are deeply copied to the new collection, which is then the sole owner of the copied objects. If the objects do not adopt theNSCopying
protocol, attempting to copy them in such a way results in a runtime error. However,copyWithZone:
produces a shallow copy. This kind of copy is only capable of producing a one-level-deep copy. If you only need a one-level-deep copy, you can explicitly call for one as inListing 2.
Listing 2 Making a deep copy
NSArray *deepCopyArray=[[NSArray alloc] initWithArray:someArray copyItems:YES];
This technique applies to the other collections as well. Use the collection’s equivalent ofinitWithArray:copyItems:
with YES
as the second parameter.
If you need a true deep copy, such as when you have an array of arrays, you can archive and then unarchive the collection, provided the contents all conform to theNSCoding
protocol. An example of this technique is shown in Listing 3
Listing 3 A true deep copy
NSArray* trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:
[NSKeyedArchiver archivedDataWithRootObject:oldArray]];
需要让自己的类对象实现coding协议
- Deep NSArray Mutable Copy Category behaviour (nsarray 深度拷贝)
- IOS深度拷贝,NSArray,NSDictionary的分类(Category)
- C#深度拷贝(Deep Copy)
- 关于NSArray的copy
- NSArray copy 问题
- Lua实现深度拷贝(Deep Copy)
- NSArray
- NSArray
- NSArray
- NSArray
- NSArray
- NSArray
- NSARRAY
- NSArray
- NSArray
- NSArray
- NSArray
- NSArray
- Oracle中Decode()函数使用技巧
- struts2 通配符和属性技巧
- Linux环境下C编程指南(第二版) -- 内存管理
- 一个优秀的Java程序员必须了解GC的工作原理
- linux-shell:shell脚本范例
- Deep NSArray Mutable Copy Category behaviour (nsarray 深度拷贝)
- oracle提高查询效率
- Android模拟器中卸载系统自带程序(转)
- Qt 内存管理机制
- 找一个字符串中有多少个字母,并输出来
- Java文件操作
- 软件分享 pqmagic 8.0中文版优秀硬盘分区管理工具
- 如何安装交叉工具链arm-linux-gcc
- Exteernal table ORA-29913,ORA-30653,KUP-01005