1

I am new to iOS and want to create an NSArray like this which contains an NSDictionary.

[
   {
     Image: 1, 2,3
     Title: 1,2,3
     Subtitle:1,2,3
   }
]

I have tried this.

NSArray *obj-image=@[@"Test.png",@"Test.png",@"Test.png"];
NSArray *obj-title=@[@"Test",@"Test",@"Test"];
NSArray *obj-subtitle=@[@"Test",@"Test",@"Test"];
NSDictionary * obj_dictionary ={ image : obj_image, title:obj_title, subtitle:obj_subtitle}
NSArray * obj_array= [obj_dictionarry];

But not working and how to access them.

Abizern
  • 146,289
  • 39
  • 203
  • 257
Khyati Vaishnav
  • 43
  • 1
  • 11

7 Answers7

1

First of all, you initialization of Arrays and Dictionaries is wrong. You cannot use "-" in the names, period.

Second, you need to allocate and then initialize the objects. This is how you do that with arrays:

NSArray *images = [NSArray arrayWithObjects: @"TestImage",@"TestImage",@"TestImage",nil];
NSArray *titles = [NSArray arrayWithObjects: @"TestTitle",@"TestTitle",@"TestTitle",nil];
NSArray *subtitles = [NSArray arrayWithObjects: @"TestSubTitle",@"TestSubTitle",@"TestSubTitle",nil];

Then you need Mutable dictionary and mutable arrays to work with the data (mutable means you can change the values inside, add or remove objects etc.)

This is the most basic example of what you are trying to achieve:

NSArray *images = [NSArray arrayWithObjects: @"TestImage",@"TestImage",@"TestImage",nil];
NSArray *titles = [NSArray arrayWithObjects: @"TestTitle",@"TestTitle",@"TestTitle",nil];
NSArray *subtitles = [NSArray arrayWithObjects: @"TestSubTitle",@"TestSubTitle",@"TestSubTitle",nil];

NSMutableArray *objectsMutable = [[NSMutableArray alloc] init];

for (NSString *string in images) {

    NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc] init];
    [dictMutable setObject:string forKey:@"image"];

    //determining the index of the image
    NSInteger stringIndex = [images indexOfObject:string];

    [dictMutable setObject:[titles objectAtIndex:stringIndex] forKey:@"title"];
    [dictMutable setObject:[subtitles objectAtIndex:stringIndex] forKey:@"subtitle"];

    NSDictionary *dict = [[NSDictionary alloc] init];
    dict = dictMutable;

    [objectsMutable addObject:dict];
}

NSArray *objects = objectsMutable;

NSLog(@"%@", objects);

Hope this helps.

As you can see, I'm going through the images array, capturing the index of each one, an then just apply values of other arrays from the same index into a mutable dictionary.

All I do after that is just make a regular dictionary and array to put the data inside. This is ho the Log will look:

(
    {
    image = TestImage;
    subtitle = TestSubTitle;
    title = TestTitle;
},
    {
    image = TestImage;
    subtitle = TestSubTitle;
    title = TestTitle;
},
    {
    image = TestImage;
    subtitle = TestSubTitle;
    title = TestTitle;
}

)

You have an array with three objects inside, each with their own image, title and subtitle.

user2695712
  • 193
  • 1
  • 9
0

Here is code :

NSMutableArray *array = [[NSMutableArray alloc] init];
NSMutableDictionary *mdict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"object1",@"key1",@"object2",@"key2",nil];
[array addObject:mDict];

so now if u need to access dictionary from array then :

NSMutableDictionary *mDict1 =  [array objectatindex:0];
NSLog(@"%@",[mDict1 valueForkey:@"key1"];

--> print object 1.

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
  • obj_image = @[@"Test.png",@"Test.png",@"Test.png"]; obj_title = @[@"Test",@"Test",@"Test"]; obj_subtitle = @[@"Test",@"Test",@"Test"]; obj_dictionary =@{ @"image" : obj_image, @"title":obj_title, @"subtitle":obj_subtitle}; obj_main=[NSMutableArray alloc]; [obj_main addObjectsFromArray:obj_dictionary]; – Khyati Vaishnav Feb 25 '16 at 10:39
  • This is the the i have created and want to acces by using this code.custom.title.text=[obj_main objectforKeyValue @"Title"]; – Khyati Vaishnav Feb 25 '16 at 10:39
  • But still not, i want Multiple value for a si ngle key – Khyati Vaishnav Feb 25 '16 at 10:40
  • multiple value from a single key .. this is not possible . dictionary u need to have unique key otherwise no use of it . then better to store array within array .. – Shobhakar Tiwari Feb 25 '16 at 10:41
  • then u should add add mulitple value in array and this array as an object needs to add in dictionary so when u get a single key : u will get an arry containing mulitple values . – Shobhakar Tiwari Feb 25 '16 at 10:48
0

This is the way to store array of dictionaries:

NSDictionary *dic=@{@"kishore":@"hai"};
 NSMutableArray *arr=[[NSMutableArray alloc]init];
 [arr addobject:dic];

this is the way to get those values:

[arr objectforkeyValue @"kishore"];
Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47
0
NSMutableArray *dictArray = [[NSMutableArray alloc] init];       // created and initiated mutable array
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];  // created and initiated mutable Dictionary
[dict setObject:@"object1" forKey:@"1"];                         // added a key pair in dictionary (you can set multiple objects)
[dictArray addObject:dict];                                      // added dictionary to array (you can add multiple dictionary to array )
NSLog(@"dictionary inside an array : %@",dictArray[0][@"1"]);    // access dictionary from array   
Madan gupta
  • 668
  • 6
  • 19
0

You can try something like this

[NSArray arrayWithObjects:@{@"Image":@[@1,@2,@3]},@{@"Title":@[@1,@2,@3]},@{@"SubTitle":@[@1,@2,@3]}, nil];
Rahul Patel
  • 1,822
  • 12
  • 21
0

first of all u need to declare correct variable name

NSArray *objImage=@[@"Test.png",@"Test.png",@"Test.png"];
NSArray *objTitle=@[@"Test",@"Test",@"Test"];
NSArray *objSubtitle=@[@"Test",@"Test",@"Test"];

at this point all u are created all the array, and u need to create dictionary like below

NSDictionary *obj_dictionary = @{@"image":objImage,@"title":objTitle, @"subtitle":objSubtitle};
// NSArray * obj_array = obj_dictionary[@"image"];
NSArray * obj_array = @[obj_dictionary]; //u can crate array of dictionary like this

in above obj_dictionary will contains all the array like below,

   Title = (
      Test,
      Test,
      Test
   );
   image =     (
      "Test.png",
      "Test.png",
      "Test.png"
  );
  subtitle =     (
      Test,
      Test,
      Test
  );

and u can access the object in the dictionary like below using a key for example

 NSArray * obj_array_images = obj_dictionary[@"image"];

gives an array of images that is associated with key image, similarly u can access other array like this by providing different keys associated with the dictionary obj_dictionary for example

 NSArray * obj_array_titles = obj_dictionary[@"title"];
 NSArray * obj_array_subtitles = obj_dictionary[@"subtitle"];

edit

NSArray *objImage=@[@"Test.png",@"Test.png",@"Test.png"];
NSArray *objTitle=@[@"Test",@"Test",@"Test"];
NSArray *objSubtitle=@[@"Test",@"Test",@"Test"];
NSDictionary *obj_dictionary = @{@"image":objImage,@"title":objTitle, @"subtitle":objSubtitle};
// NSArray * obj_array = obj_dictionary[@"image"];
NSArray * obj_array = @[obj_dictionary]; //u can crate array of dictionary like this
Shankar BS
  • 8,394
  • 6
  • 41
  • 53
0

You can easily create objects of array and access them back using following way.

    NSArray *objimage=@[@"Test1.png",@"Test2.png",@"Test3.png"];
    NSArray *objtitle=@[@"Test1",@"Test2",@"Test3"];
    NSArray *objsubtitle=@[@"Test1",@"Test2",@"Test3"];

    NSDictionary *obj_dictionary = @{@"image":objimage,@"Title":objtitle, @"subtitle":objsubtitle};

    NSArray * obj_array= [[NSArray alloc] initWithObjects:obj_dictionary, nil]; // Create Array of nested objects
    if([obj_array count] > 0) {
        NSArray * imageArray = obj_array[0][@"image"]; // Access the nested objects in Array.
        NSLog(@"%@", imageArray[0]);
    }
Vignesh Kumar
  • 598
  • 4
  • 11