Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 208
Naveen Avatar asked Jul 07 '26 22:07

Naveen


2 Answers

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.

like image 197
Anoop Vaidya Avatar answered Jul 10 '26 15:07

Anoop Vaidya


Like this:

NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:images andKeys:names];
like image 31
dasdom Avatar answered Jul 10 '26 14:07

dasdom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!