Reputation: 1251
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
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
Reputation: 5377
Like this
NSDictionary * list = [NSDictionary dictionaryWithObjects:images forKeys:names];
Upvotes: 2
Reputation: 14073
Like this:
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:images andKeys:names];
Upvotes: 2