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.

1
  • Are you trying to parse JSON? Commented Feb 25, 2016 at 11:03

7 Answers 7

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

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];
This is the the i have created and want to acces by using this code.custom.title.text=[obj_main objectforKeyValue @"Title"];
But still not, i want Multiple value for a si ngle key
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 ..
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 .
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"];

Comments

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   

Comments

0

You can try something like this

[NSArray arrayWithObjects:@{@"Image":@[@1,@2,@3]},@{@"Title":@[@1,@2,@3]},@{@"SubTitle":@[@1,@2,@3]}, nil];

Comments

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

Comments

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

4 Comments

NSArray * the whole dictonaryobj_array = obj_dictionary[@"image"]; It only store value of Image not
sorry, i didn't check at the final result i commented line and added new line check
NSArray * obj_array_images = obj_dictionary[@"image"] This can acces whole image, How to Get particular image as indexpath.row is not working
NSArray * obj_array_images = obj_dictionary[@"image"] give array u can access the particular object like obj_array_images[ indexpath.row ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.