gpt4 book ai didi

objective-c - NSMutableDictionary 添加子项

转载 作者:行者123 更新时间:2023-12-03 17:52:05 24 4
gpt4 key购买 nike

关于如何获取 NSMutableDictionary 并使其成为另一个我似乎无法理解的 NSMutableDictionary 的子元素,肯定缺少一些关键元素。我已经尝试了几乎所有方法,并且开始认为跳桥可能会解决问题。

当前:

DICT:1 root
|_main dict

DICT:2 root
|_dict [A]
|
sub dict...

期望:

DICT:1 root
|_main dict
|_dict [A]
|
sub dict....

每当我尝试将“DICT:2”添加到“DICT:1”时,它都会覆盖主词典。留给我:

DICT:1 root
|_dict [A]
|
sub dict...

我尝试了很多事情,包括:

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];

[myDict setObject:@"mainDict" forKey:@"mainDict"];
[myDict setObject:@"dictA" forKey:@"mainDict"];

也尝试过:

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];

[myDict addEntriesFromDictionary:dictA];

NSMutableDictionary *moarDict = [myDict objectForKey:@"mainDict"];
[moarDict addEntriesFromDictionary:dictA];

和:

moarDict = [dictA mutableCopy];
呃! NSMutableDictionary 一定讨厌我,因为毫无疑问我讨厌它。

最佳答案

试试这个:

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *mainDict = [[NSMutableDictionary alloc] init];
[myDict setObject:mainDict forKey:@"mainDict"];
[myDict[@"mainDict"] setObject:dictA" forKey:@"dictA"];

这会导致

myDict
| mainDict
| dictA
| sub dict

关于objective-c - NSMutableDictionary 添加子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23587883/

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