gpt4 book ai didi

ios - 将数据添加到另一个 NSDictionary 中的 NSDictionary

转载 作者:行者123 更新时间:2023-11-28 21:15:53 27 4
gpt4 key购买 nike

我正在尝试将数据添加到嵌套在其他词典中的 NSMutableDictionary

在添加数据的代码运行之前开始输出

DayData Dictionary {            //DayData Dictionary

Monday = { //events Dictionary
trip1 = { //eventData Dictionary
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
};
Thursday = {
trip1 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
};
Tuesday = {
trip1 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
};
Wendsday = {
trip1 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
};
}

生成此输出的代码

NSMutableDictionary *eventData = [[NSMutableDictionary alloc]initWithObjects:@[@"5:00",@"9items",@"beachfucking"] forKeys:@[@"time",@"numItems",@"tripName"]];

NSMutableDictionary *event = [NSMutableDictionary dictionaryWithObjectsAndKeys:
eventData,@"trip1",nil];

NSMutableDictionary *dayData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
event,@"Monday",event,@"Tuesday",
event,@"Wendsday",event,@"Thursday",nil];

我目前正在尝试解决这个问题。在这里,我创建了一个新事件 NSDictionary,然后将该数据添加到 DayData 字典中。但是输出不正确。一周中的每一天都得到一个“trip2”数据集,而星期一只是假设。

 NSMutableDictionary *event2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
eventData,@"trip2",nil];
[[dayData objectForKey:@"Monday"] addEntriesFromDictionary:event2];

上面代码的当前错误输出

2016-12-24 01:56:41.261329 test1[10098:380349] {
Monday = {
trip1 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
trip2 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
};
Thursday = {
trip1 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
trip2 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
};
Tuesday = {
trip1 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
trip2 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
};
Wendsday = {
trip1 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
trip2 = {
numItems = 9items;
time = "5:00";
tripName = beachfucking;
};
};
}

注意 trip2 是如何为一周中的所有天填写的。

最佳答案

您所有的天键都包含相同的引用对象事件,因此更改其中之一,将反射(reflect)到所有天的键。

要解决这个问题,请像这样声明您的 dayData Dictionary

NSMutableDictionary *dayData = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[event mutableCopy] ,@"Monday",[event mutableCopy],@"Tuesday",
[event mutableCopy],@"Wendsday",[event mutableCopy],@"Thursday",nil];

关于ios - 将数据添加到另一个 NSDictionary 中的 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41311153/

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