Manigandan S Manigandan S - 4 months ago 14
Objective-C Question

How can i create NSDictionary with multiple array of key and value

i tried to create static JSONArray of value in IOS using Objective -c

i want like this

tabledata={
["name":"image 1","path":"img1.jpg"],
["name":"image 2","path":"img2.jpg"],
["name":"image 3","path":"img3.jpg"],
["name":"image 4","path":"img4.jpg"],
["name":"image 5","path":"img5.jpg"],
["name":"image 6","path":"img6.jpg"],
["name":"image 7","path":"img7.jpg"]}


this is my data.. please help me any one how can i declare in objective-c..

Answer

You can create dictionary like below

NSDictionary *dict = @{
       @"array": @[
           @{
               @"name":@"image 1",
               @"path":@"img1.jpg"
            },
            @{
               @"name":@"image 2",
               @"path":@"img2.jpg"
            }
            ....
        ]
    };

and Array

NSArray *array = @[
           @{
               @"name":@"image 1",
               @"path":@"img1.jpg"
            },
            @{
               @"name":@"image 2",
               @"path":@"img2.jpg"
            }
            ....
        ];

For get value from NSDictionary

NSArray *array = NSDictionary[@"array"]
NSDictionary *firstObj = array[0];
NSString *name = firstObj[@"name"]
NSString *path = firstObj[@"path"]

from Array just

NSDictionary *firstObj = array[0];
NSString *name = firstObj[@"name"]
NSString *path = firstObj[@"path"]