iPhone: How to create a NSMutableDictionary which contains multiple sets of data with same key?

{

"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?

Answers


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];

Need Your Help

Creating windows of a sequence in r

r substring

I have a character string for which I want to store all possible windows of a given size. In my first approach I have done the following to accomplish this:

Tinyurl API Example - Am i doing it right :D

php api tinyurl

we use super-long Hashes for the Registration of new Users in our Application. The Problem is that these Hashes break in some Email Clients - making the Links unusable.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.