gpt4 book ai didi

iphone - 修改新词典的内容也会修改父词典数据

转载 作者:行者123 更新时间:2023-11-28 20:23:33 25 4
gpt4 key购买 nike

由于我需要将相似的对象添加到数组中,所以我以这种方式创建了新的字典。

NSMutableDictionary* existingStepDict = [[[arrayForSteps objectAtIndex:0] mutableCopy] autorelease];
[arrayForSteps addObject:existingStepDict];
[existingStepDict release];

现在,这里发生的事情是,稍后当我更改字典中的任何一个内容时,另一个字典也会更新。我要求这两个词典都独立运行。

为此,我经历了字典的深度复制,其代码是这样的。

   NSMutableDictionary* existingStepDict = [[[arrayForSteps objectAtIndex:0] mutableCopy] autorelease];

NSMutableDictionary* destination = [NSMutableDictionary dictionaryWithCapacity:0];

NSDictionary *deepCopy = [[NSDictionary alloc] initWithDictionary:existingStepDict copyItems: YES];
if (deepCopy) {
[destination addEntriesFromDictionary: deepCopy];
[deepCopy release];
}
//add Properties array to Steps Dictionary
[arrayForSteps addObject:destination];

但这也没有反射(reflect)出差异。我知道我在这里犯了一些小错误。但是有人可以帮助我得到结果吗?

非常感谢!

最佳答案

有一种使用 NSCoding(序列化)协议(protocol)获取 NSDictionary 或 NSArray 的完整深拷贝的简单方法。

- (id) deepCopy:(id)mutableObject
{
NSData *buffer = [NSKeyedArchiver archivedDataWithRootObject:mutableObject];
return [NSKeyedUnarchiver unarchiveObjectWithData: buffer];
}

通过这种方式,您可以一步复制任何对象及其包含的所有对象。

关于iphone - 修改新词典的内容也会修改父词典数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14974823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com