iPhone:如何创建一个包含多组具有相同键的数据的 NSMutableDictionary?

发布于 2024-11-09 03:02:33 字数 1094 浏览 2 评论 0原文

{

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2024-11-16 03:02:33

请注意,最低级别字典中的第一个对象是列表或数组。这是关键。

 NSMutableArray * dataArr = [NSMutableArray array];
 NSArray * x_labelArr = [NSArray arrayWithObjects: ...];
 NSMutableDictionary * dict = 
            [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                     arr, @"data", x_labelArr, @"x_labels", nil];

现在您可以将单独的内部字典添加到数组中,每个内部字典都具有相同的键:

NSDictionary * innerDict = 
            [NSDictionary dictionaryWithObjectsAndKeys: 
                  someArrayOfData, @"data", someTitle, @"title"];
NSDictionary * innerDict2 = 
            [NSDictionary dictionaryWithObjectsAndKeys: 
                            someOtherArrayOfData, @"data", 
                                 someOtherTitle, @"title"];
[dataArr addObject:innerDict];
[dataArr addObject:innerDict2];

Notice that the first object in the lowest-level dictionary is a list or array. This is the key.

 NSMutableArray * dataArr = [NSMutableArray array];
 NSArray * x_labelArr = [NSArray arrayWithObjects: ...];
 NSMutableDictionary * dict = 
            [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                     arr, @"data", x_labelArr, @"x_labels", nil];

Now you can add individual inner dictionaries to the array, each of which has the same keys:

NSDictionary * innerDict = 
            [NSDictionary dictionaryWithObjectsAndKeys: 
                  someArrayOfData, @"data", someTitle, @"title"];
NSDictionary * innerDict2 = 
            [NSDictionary dictionaryWithObjectsAndKeys: 
                            someOtherArrayOfData, @"data", 
                                 someOtherTitle, @"title"];
[dataArr addObject:innerDict];
[dataArr addObject:innerDict2];
瞎闹 2024-11-16 03:02:33

您可以将词典添加到您的词典中;字典都包含“数据”->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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文