Question

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);
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top