iPhone:如何创建一个包含多组具有相同键的数据的 NSMutableDictionary?
{
"data": [
{
"data": [
null,
null,
30,
45,
69,
70,
65
],
"title": "title5"
},
{
"data": [
null,
5,
10,
15,
22,
30
],
"title": "title4"
},
{
"data": [
40,
55,
],
"title": "title3"
},
{
"data": [
null,
89,
90,
85
],
"title": "title2"
},
{
"data": [
66,
77,
55,
33,
50,
-6,
-8
],
"title": "title1"
}
],
"x_labels": [
6,
7,
8,
9,
10,
11,
12
]
}
我希望我的字典对于它包含的每组值都有相同的键,称为“数据”和“标题”,如上面的代码所示。
但是,当我尝试创建字典并尝试对同一键使用 setValue:forKey:
方法或 addEntriesFromDictionary:
时,它会覆盖每组数据,最后我只得到最后一个我将其放入字典中的数据集。
有什么办法,我可以做到这一点吗?
{
"data": [
{
"data": [
null,
null,
30,
45,
69,
70,
65
],
"title": "title5"
},
{
"data": [
null,
5,
10,
15,
22,
30
],
"title": "title4"
},
{
"data": [
40,
55,
],
"title": "title3"
},
{
"data": [
null,
89,
90,
85
],
"title": "title2"
},
{
"data": [
66,
77,
55,
33,
50,
-6,
-8
],
"title": "title1"
}
],
"x_labels": [
6,
7,
8,
9,
10,
11,
12
]
}
I want my dictionary to have same key called "data" and "title" for each set of values it contains as shown by above code.
But when I try to create a dictionary and try using setValue:forKey:
method for the same key or addEntriesFromDictionary:
it overwrites each set of data and finally I only get the last set of data which I pushed into my dictionary.
Is there a way, I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请注意,最低级别字典中的第一个对象是列表或数组。这是关键。
现在您可以将单独的内部字典添加到数组中,每个内部字典都具有相同的键:
Notice that the first object in the lowest-level dictionary is a list or array. This is the key.
Now you can add individual inner dictionaries to the array, each of which has the same keys:
您可以将词典添加到您的词典中;字典都包含“数据”->asdf、“标题”->asdf。或者,为“数据”元素添加一个数组并添加到其中。但从您的数据来看,您更有可能需要这个。
You can add dictionaries to your dictionary; the dictionaries all contain "data"->asdf, "title"->asdf. Or, add an array for a "data" element and add to that. But from what your data looks like, it's more likely you want this.