2014-01-14 28 views
-1

我如何從第一個'Item 0'提取'選擇'到數組中? enter image description here如何從此plist字典中提取數組?

此刻,打印數組爲null。

NSDictionary *dataDictionary = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"pollData" ofType:@"plist"]]; 

    NSMutableArray *arrayOfChoices = [[dataDictionary objectForKey:@"Item 0"] objectForKey:@"choices"]; 
    self.dataArray = arrayOfChoices; 

    NSLog(@"array: %@", self.dataArray); 
+1

嗯,一件事,它不是一個數組。更重要的是,'dataDictionary'不是一本字典;根對象是一個數組。 –

+0

對不起,我忽略了這一點。但我把它改成了一個數組,但它仍然不起作用。 – jsky

+0

更改了數組? –

回答

3

根對象是一個數組,而不是一個字典。

NSArray * pollData = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pollData" ofType:@"plist"]]; 

對象在該陣列中的索引0是一個字典,由於是內部的,其鍵接到@"choices"的對象。

NSDictionary * choices = [[pollData objectAtIndex:0] objectForKey:@"choices"]; 

從plists反序列化的對象永遠不可變。如果你想要一個可變的字典,你需要製作一個可變的副本。