Reputation: 225
My .plist starts with an Array Root and then has multiple keys of dictionaries containing six items each.
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"];
self.questions = [NSArray arrayWithContentsOfFile:path];
NSDictionary *firstQuestion = [self.questions objectAtIndex:0];
self.questionLabel.text = [firstQuestion objectForKey:@"QuestionTitle"];
[self.ansOne setTitle:[firstQuestion objectForKey:@"A"] forState:UIControlStateNormal];
[self.ansTwo setTitle:[firstQuestion objectForKey:@"B"] forState:UIControlStateNormal];
[self.ansThree setTitle:[firstQuestion objectForKey:@"C"] forState:UIControlStateNormal];
[self.ansFour setTitle:[firstQuestion objectForKey:@"D"] forState:UIControlStateNormal];
But I load blank labels, here's the XML of my .plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>QuestionTitle</key>
<string>Which of the following probes will achieve the highest penetration?</string>
<key>A</key>
<string>5.0 Mhz</string>
<key>B</key>
<string>4.0 Mhz</string>
<key>C</key>
<string>3.4 Mhz</string>
<key>D</key>
<string>1.0 Mhz</string>
<key>QuestionAnswer</key>
<string>D</string>
</dict>
<dict>
<key>QuestionTitle</key>
<string></string>
<key>A</key>
<string></string>
<key>B</key>
<string></string>
<key>C</key>
<string></string>
<key>D</key>
<string></string>
<key>QuestionAnswer</key>
<string></string>
</dict>
<dict>
<key>QuestionTitle</key>
<string></string>
<key>A</key>
<string></string>
<key>B</key>
<string></string>
<key>C</key>
<string></string>
<key>D</key>
<string></string>
<key>QuestionAnswer</key>
<string></string>
</dict>
</array>
</plist>
I was following a tutorial and many other people were facing this issue, can anyone point me in the right direction, or fix the code for me please?
Thanks in advance.
Upvotes: 0
Views: 132
Reputation: 1019
My app does the same thing yours is trying to do except my plist root is a dictionary instead of an array.
Instead of:
self.questions = [NSArray arrayWithContentsOfFile:path];
Use:
NSDictionary* questions = [NSDictionary dictionaryWithContentsOfFile:path];
And then to get to the question you want to, use:
NSDictionary *firstQuestion = [NSDictionary dictionaryWithDictionary:[questions objectForKey:[NSString stringWithFormat:@"0"]]];
Upvotes: 1
Reputation: 3545
I faced the same issue, I fixed it by using NSPropertyListSerialization like this:
NSString *error;
NSPropertyListFormat format;
id obj = [NSPropertyListSerialization propertyListFromData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Questions" ofType:@"plist"]] mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&error];
if ([obj isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:obj];
} else {
NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:obj];
}
Upvotes: 1