2011-03-09 63 views
1

如何從特定鍵訪問單個鍵值?如何訪問字典數組中的特定鍵值

我正在使用NSDictionary的NSArray與plist源代碼,如下所示;

<plist version="1.0"> 
<array> 
<dict> 
    <key>name</key> 
    <string>item name</string> 
    <key>level</key> 
    <integer>3</integer> // range is 1 .. 7 
    <key>Stage1</key> 
    <string>Text for Stage 1</string> 
    <key>Stage2</key> 
    <string>Text for Stage 2</string> 
    <key>Stage3</key> 
    <string>Text for Stage 3</string> 
    <key>Stage4</key> 
    <string>Text for Stage 4</string> 
    <key>Stage5</key> 
    <string>Text for Stage 5</string> 
    <key>Stage6</key> 
    <string>Text for Stage 6</string> 
    <key>Stage7</key> 
    <string>Text for Stage 7</string> 
</dict> 
// more dictionaries follow 

我可以通過執行以下操作查看整部字典:

NSNumber *helps = [myArray objectAtIndex:[pickerView selectedRowInComponent:0]]; 
notesLabel.text = [NSString stringWithFormat:@"%@", helps]; 

你的幫助是極大的讚賞...我這樣的小白。

直到感謝您的幫助!我能夠補充:

NSDictionary *dictionary = [foodArray objectAtIndex:[pickerView selectedRowInComponent:0]]; 
notesLabel.text = (@"stage is: %@", [dictionary objectForKey:[NSString stringWithFormat:@"%@", stage]]); 

哪給了我正是我想念的東西。 :)

回答

0

檢查NSDictionary的文檔 - 專門爲 「objectForKey:」

NSDictionary *dictionary = [myArray objectAtIndex:[pickerView selectedRowInComponent:0]]; 
NSLog(@"name: %@", [dictionary objectForKey:@"name"]); 
NSLog(@"level: %@", [dictionary objectForKey:@"level"]); 
+0

截止,感謝您的幫助! – padapa 2011-03-09 23:02:04

+1

就像一個魅力,只是我失蹤了。 – padapa 2011-03-14 12:36:58