Naveen
Naveen

Reputation: 1251

NSDictionary and NSArray

I have arrays of names and images like below

NSArray *names = [[NSArray alloc] initWithObjects:@"naveen", @"kumar",nil];
NSArray *images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:@"1.jpg"], [UIImage imageNamed:@"2.jpg"], nil];

I want to create a dictionary in the following format

list:
  item 0 : naveen
       1.jpg
  item 1: kumar
       2.jpg

How can i create this one? Please?

Upvotes: 3

Views: 288

Answers (3)

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

You need to do like this :

NSMutableDictionary *nameImageDict=[NSMutableDictionary new];
for (NSInteger i=0; i<names.count; i++) {
    NSArray *array=@[names[i],images[i]];
    //or in older compiler 4.3 and below
    //NSArray *array=[NSArray arrayWithObjects:[names objectAtIndex:i],[images objectAtIndex:i], nil];
    [nameImageDict setObject:array forKey:[NSString stringWithFormat:@"item %d",i]];
}

for key item 0: it will have an array. The array contains name and image.

Upvotes: 3

Desdenova
Desdenova

Reputation: 5377

Like this

NSDictionary * list = [NSDictionary dictionaryWithObjects:images forKeys:names];

Upvotes: 2

dasdom
dasdom

Reputation: 14073

Like this:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:images andKeys:names];

Upvotes: 2

Related Questions