简体   繁体   English

NSDictionary和NSArray

[英]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? 请?

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. 对于关键项0:它将有一个数组。 The array contains name and image. 该数组包含名称和图像。

像这样:

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

像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM