Gre*_*reg 20 iphone nsdictionary deep-copy nsmutabledictionary
问题:有没有办法使用现有的objective-c方法来完成NSDictionary或NSArray的完整深层副本,它们本身有嵌套的词典或数组?
那是我已经读过的问题可能是当它遇到嵌套的字典或数组时它只复制指向嵌套项的指针,而不是真正复制该项.
背景:作为一个例子,我正在尝试使用NSUserDefaults加载/保存以下配置,并且当加载需要将不可变副本转换为NSUserDefault之前进行更改之前的可变副本.
She*_*ley 34
几年前,我出于完全相同的原因编写了一些类别方法,将整个用户默认树转换为可变.他们在这里 - 使用它们需要您自担风险!:-)
//
// SPDeepCopy.h
//
// Created by Sherm Pendley on 3/15/09.
//
#import <Cocoa/Cocoa.h>
// Deep -copy and -mutableCopy methods for NSArray and NSDictionary
@interface NSArray (SPDeepCopy)
- (NSArray*) deepCopy;
- (NSMutableArray*) mutableDeepCopy;
@end
@interface NSDictionary (SPDeepCopy)
- (NSDictionary*) deepCopy;
- (NSMutableDictionary*) mutableDeepCopy;
@end
Run Code Online (Sandbox Code Playgroud)
//
// SPDeepCopy.m
//
// Created by Sherm Pendley on 3/15/09.
//
#import "SPDeepCopy.h"
@implementation NSArray (SPDeepCopy)
- (NSArray*) deepCopy {
unsigned int count = [self count];
id cArray[count];
for (unsigned int i = 0; i < count; ++i) {
id obj = [self objectAtIndex:i];
if ([obj respondsToSelector:@selector(deepCopy)])
cArray[i] = [obj deepCopy];
else
cArray[i] = [obj copy];
}
NSArray *ret = [[NSArray arrayWithObjects:cArray count:count] retain];
// The newly-created array retained these, so now we need to balance the above copies
for (unsigned int i = 0; i < count; ++i)
[cArray[i] release];
return ret;
}
- (NSMutableArray*) mutableDeepCopy {
unsigned int count = [self count];
id cArray[count];
for (unsigned int i = 0; i < count; ++i) {
id obj = [self objectAtIndex:i];
// Try to do a deep mutable copy, if this object supports it
if ([obj respondsToSelector:@selector(mutableDeepCopy)])
cArray[i] = [obj mutableDeepCopy];
// Then try a shallow mutable copy, if the object supports that
else if ([obj respondsToSelector:@selector(mutableCopyWithZone:)])
cArray[i] = [obj mutableCopy];
// Next try to do a deep copy
else if ([obj respondsToSelector:@selector(deepCopy)])
cArray[i] = [obj deepCopy];
// If all else fails, fall back to an ordinary copy
else
cArray[i] = [obj copy];
}
NSMutableArray *ret = [[NSMutableArray arrayWithObjects:cArray count:count] retain];
// The newly-created array retained these, so now we need to balance the above copies
for (unsigned int i = 0; i < count; ++i)
[cArray[i] release];
return ret;
}
@end
@implementation NSDictionary (SPDeepCopy)
- (NSDictionary*) deepCopy {
unsigned int count = [self count];
id cObjects[count];
id cKeys[count];
NSEnumerator *e = [self keyEnumerator];
unsigned int i = 0;
id thisKey;
while ((thisKey = [e nextObject]) != nil) {
id obj = [self objectForKey:thisKey];
if ([obj respondsToSelector:@selector(deepCopy)])
cObjects[i] = [obj deepCopy];
else
cObjects[i] = [obj copy];
if ([thisKey respondsToSelector:@selector(deepCopy)])
cKeys[i] = [thisKey deepCopy];
else
cKeys[i] = [thisKey copy];
++i;
}
NSDictionary *ret = [[NSDictionary dictionaryWithObjects:cObjects forKeys:cKeys count:count] retain];
// The newly-created dictionary retained these, so now we need to balance the above copies
for (unsigned int i = 0; i < count; ++i) {
[cObjects[i] release];
[cKeys[i] release];
}
return ret;
}
- (NSMutableDictionary*) mutableDeepCopy {
unsigned int count = [self count];
id cObjects[count];
id cKeys[count];
NSEnumerator *e = [self keyEnumerator];
unsigned int i = 0;
id thisKey;
while ((thisKey = [e nextObject]) != nil) {
id obj = [self objectForKey:thisKey];
// Try to do a deep mutable copy, if this object supports it
if ([obj respondsToSelector:@selector(mutableDeepCopy)])
cObjects[i] = [obj mutableDeepCopy];
// Then try a shallow mutable copy, if the object supports that
else if ([obj respondsToSelector:@selector(mutableCopyWithZone:)])
cObjects[i] = [obj mutableCopy];
// Next try to do a deep copy
else if ([obj respondsToSelector:@selector(deepCopy)])
cObjects[i] = [obj deepCopy];
// If all else fails, fall back to an ordinary copy
else
cObjects[i] = [obj copy];
// I don't think mutable keys make much sense, so just do an ordinary copy
if ([thisKey respondsToSelector:@selector(deepCopy)])
cKeys[i] = [thisKey deepCopy];
else
cKeys[i] = [thisKey copy];
++i;
}
NSMutableDictionary *ret = [[NSMutableDictionary dictionaryWithObjects:cObjects forKeys:cKeys count:count] retain];
// The newly-created dictionary retained these, so now we need to balance the above copies
for (unsigned int i = 0; i < count; ++i) {
[cObjects[i] release];
[cKeys[i] release];
}
return ret;
}
@end
Run Code Online (Sandbox Code Playgroud)
Ric*_*ckR 31
我觉得这样的事情应该有效.
NSData *buffer;
NSMutableDictionary *_dict1, *_dict2;
// Deep copy "all" objects in _dict1 pointers and all to _dict2
buffer = [NSKeyedArchiver archivedDataWithRootObject: _dict1];
_dict2 = [NSKeyedUnarchiver unarchiveObjectWithData: buffer];
Run Code Online (Sandbox Code Playgroud)
dba*_*dge 23
DEEP副本的简单方法是简单地使用CFPropertyListCreateDeepCopy.这是一个例子(使用ARC):
NSDictionary *newDictionary =
(__bridge NSDictionary *)(CFPropertyListCreateDeepCopy(kCFAllocatorDefault,
(__bridge CFPropertyListRef)(originalDictionary),
kCFPropertyListImmutable));
Run Code Online (Sandbox Code Playgroud)
我在这个例子中的originalDictionary是一个NSDictionary
只要CFPropertyListRef的顶级实体是CFDictionary,CFPropertyListRef就可以简单地转换为NSDictionary.