문제

How do i extract 'choices' from the first 'Item 0' into an array? enter image description here

At the moment, printing the array gives 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);
도움이 되었습니까?

해결책

The root object is an array, not a dictionary.

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

The object at index 0 in that array is a dictionary, as is the object inside that which is keyed to @"choices".

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

Objects that are unserialized from plists are never mutable. If you want a mutable dictionary, you need to make a mutable copy.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top