How to create an NSDictionary with multiple keys?

前端 未结 5 822
臣服心动
臣服心动 2020-12-30 12:34

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

相关标签:
5条回答
  • 2020-12-30 13:12

    if you want multiple categories , you can follow this format

    NSDictionary *jsonObject = @{
                               @"data1":@[
                                  @{
                                    @"title":@"A"
    
                                    @"subData" : @[
                                    @{
                                      @"title":@"aa"
                                     }]
                                    }
    
                                 ]
                             };
    
    0 讨论(0)
  • 2020-12-30 13:20

    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"]);
    
    0 讨论(0)
  • 2020-12-30 13:21

    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"];
    
    0 讨论(0)
  • 2020-12-30 13:21

    Here is the structure:
    Your root object is NSMutableDictionary
    eventData - key for object NSMutableDictionary with keys and objects:
    ->key eventDate object NSString
    ->key eventLocation object NSMutableDictionary with keys and objects:
    ----> key latitude object NSNumber
    ----> key longitude object NSNumber
    -> key text object NSString
    -> key imageData object NSString later converted to NSData
    -> key imageFormat object NSString
    -> key expirationTime object NSNumber
    type key for object NSString
    title key for object NSString

    0 讨论(0)
  • 2020-12-30 13:28

    How to Create NSArray and with Access for object using NSDictionary ?

    ... Create NSArray

    NSArray *studentkeys = [NSArray arrayWithObjects:@"studentName", @"studentBirthDate", @"studentCity", @"studentMobile"  nil];
    
    NSArray *objects = [NSArray arrayWithObjects:@"Pravin", @"27/08/1990", @"Bhavnagar",@"7878007531", nil]; 
    

    ...to Access to NSArray Object Using NSDictionary

    NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];                                  
    
    0 讨论(0)
提交回复
热议问题