2013-03-21 101 views
3

我有名字和图像阵列像下面的NSDictionary的NSArray和

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

我想创建一个字典的格式如下

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

我怎么能创造这一个?请?

+0

我不明白您的格式。你可以用JSON编写它吗? – dasdom 2013-03-21 08:32:27

+0

@Naveen:你想把“item 1”作为key,'Kumar'和'2.jpg'作为值吗? – 2013-03-21 08:36:24

+0

https://www.google.co.in/search?hl=zh-CN&client=firefox-a&hs=ANc&rls=org.mozilla:en-US:official&channel=fflb&q=simple+plist&bav=on.2,or.r_qf.&bvm = bv.44158598,d.bmk&BIW = 1202&波黑= 573&微米= 1&即= UTF-8&TBM = isch&源= OG&SA = N&标签=无线&EI = W8ZKUae5IYesrAfr4YGQBg#imgrc = jZoy6pXpdEpf0M%3A%3BVl3xb9zwub3DfM%3Bhttp%253A%252F%252Fi.stack.imgur.com %252Fz0fAm.png%3Bhttp%253A%252F%252Fstackoverflow.com%252Fquestions%252F13616206%252Fcopying-objects-from-plist-file-to-another-plist-file%3B438%3B261检查链接,而不是greenday我需要item0像th – Naveen 2013-03-21 08:39:14

回答

3

你需要做的是这样的:

NSMutableDictionary *nameImageDict=[NSMutableDictionary new]; 
for (NSInteger i=0; i<names.count; i++) { 
    NSArray *[email protected][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]]; 
} 

重点项目0:它有一个数组。该数组包含名称和图像。

+0

这是什么NSArray * array = @ [names [i],images [i]] ; ? – Naveen 2013-03-21 08:45:49

+0

检查我的编辑答案。并在这里看到http://clang.llvm.org/docs/ObjectiveCLiterals.html – 2013-03-21 08:50:57

+0

@Naveen:你检查过其他旧代码吗? – 2013-03-21 10:22:22

2

像这样:

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

像这样

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

这很有趣!我们同时写了同样的内容。 – dasdom 2013-03-21 08:25:25

+0

你用几秒钟打我,我想:) – Desdenova 2013-03-21 08:25:51

+0

哦,我的错了! – dasdom 2013-03-21 08:27:40