2013-03-21 102 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