Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an NSDictionary with multiple keys?

I am not sure if what I am going to ask is actually an NSDictionary with multiple keys but ok.

What I want to do is create an NSDictionary with keys and values for my data and then convert it to JSON format. The JSON format would look exactly like this :

{
    "eventData": {
        "eventDate": "Jun 13, 2012 12:00:00 AM",
        "eventLocation": {
            "latitude": 43.93838383,
            "longitude": -3.46
        },
        "text": "hjhj",
        "imageData": "raw data",
        "imageFormat": "JPEG",
        "expirationTime": 1339538400000
    },
    "type": "ELDIARIOMONTANES",
    "title": "accIDENTE"
}

I ve only used NSDictionaries like this :

NSArray *keys = [NSArray arrayWithObjects:@"eventDate", @"eventLocation", @"latitude"  nil];
NSArray *objects = [NSArray arrayWithObjects:@"object1", @"object2", @"object3", nil]; 
dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

But the above format is not all about key - value. So my question is how would the NSDictionary be , to fit the JSON format?? Thanks for reading my post , and sorry if any confusion.

like image 245
donparalias Avatar asked Jun 26 '12 11:06

donparalias


People also ask

How do I create an NSDictionary in Objective C?

Creating NSDictionary Objects Using Dictionary Literals In addition to the provided initializers, such as init(objects:forKeys:) , you can create an NSDictionary object using a dictionary literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:forKeys:count:) method.

What is the difference between NSMapTable vs NSDictionary?

NSDictionary / NSMutableDictionary copies keys, and holds strong references to values. NSMapTable is mutable, without an immutable counterpart. NSMapTable can hold keys and values with weak references, in such a way that entries are removed when either the key or value is deallocated.

How do you set a value in NSDictionary?

You have to convert NSDictionary to NSMutableDictionary . You have to user NSMutableDictionary in place of the NSDictionary . After that you can able to change value in NSMutableDictionary .

What is the difference between NSDictionary and NSMutableDictionary?

Main Difference is:NSMutableDictionary is derived from NSDictionary, it has all the methods of NSDictionary. NSMutableDictionary is mutable( can be modified) but NSDictionary is immutable (can not be modified).


2 Answers

You can have a NSDictionary inside another NSDictonary:

NSDictionary *eventLocation = [NSDictionary dictionaryWithObjectsAndKeys:@"43.93838383",@"latitude",@"-3.46",@"latitude", nil];

NSMutableDictionary *eventData = [NSDictionary dictionaryWithObjectsAndKeys:eventLocation,@"eventLocation", nil];
[eventData setObject:@"Jun 13, 2012 12:00:00 AM" forKey:@"eventDate"];
[eventData setObject:@"hjhj" forKey:@"text"];
.
.
.
NSMutableDictionary *finalDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:eventData,@"eventData", nil];
[finalDictionary setObject:@"ELDIARIOMONTANES" forKey:@"type"];
[finalDictionary setObject:@"accIDENTE" forKey:@"title"];
like image 53
Rui Peres Avatar answered Sep 21 '22 15:09

Rui Peres


Now with Objective-C literals there is a much better, easier, and cleaner way of accomplishing this. Here is your exact dictionary with this new syntax:

NSDictionary *dictionary = @{
    @"eventData": @{
        @"eventDate": @"Jun 13, 2012 12:00:00 AM",
        @"eventLocation": @{
            @"latitude": @43.93838383,
            @"longitude": @-3.46
        },
        @"text": @"hjhj",
        @"imageData": @"raw data",
        @"imageFormat": @"JPEG",
        @"expirationTime": @1339538400000
    },
    @"type": @"ELDIARIOMONTANES",
    @"title": @"accIDENTE"
};

// Prints: "43.93838383"
NSLog(@"%@", dictionary[@"eventData"][@"eventLocation"][@"latitude"]);
like image 28
Firo Avatar answered Sep 17 '22 15:09

Firo