Question

I'm trying to search through the plist, find if Available is true. here's my plist.

Image of plist: http://imgur.com/24vtIEW

After searching the Cocos2D forums and here, I've managed to attempt my own effort but not getting very far with it.

Here's the code.

NSString  *fullPathToPList = [[NSBundle mainBundle] pathForResource:@"missions" ofType:@"plist"];
NSArray  *MissionsList;
NSDictionary  *plistDict;
NSInteger     pIdx, mCount;

plistDict = [NSDictionary dictionaryWithContentsOfFile: fullPathToPList];
MissionsList = [plistDict valueForKey: @"Missions"];
mCount = [MissionsList count];
NSLog(@"Number of Missions in plist = %d", mCount);

for (pIdx = 0; pIdx < mCount; pIdx++)
{
NSDictionary *eachMission = [MissionsList objectAtIndex: pIdx];
NSString *MissionName = [eachMission valueForKey: @"mission name"];
NSString *Available = [eachMission valueForKey: @"Available"];

NSLog(@"\nMission[%d] \nName = %@, \nAvailable = %@", pIdx, MissionName, Available);
}

What i'd like to be happening here, is for it to be returning

Mission 1
Name = First Mission
Available = 1

Any help in the right direction would be greatly appreciated.

Was it helpful?

Solution

According to your picture Missions is a dictionary rather than an array. So you need to read them differently.

NSDictionary *plistDict = ...;
NSDictionary *missions = [plistDict objectForKey:@"Missions"];
[missions enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSDictionary *mission, BOOL *stop) {
    NSString *missionName = [mission objectForKey:@"mission name"];
    NSNumber *available = [mission objectForKey:@"Available"];

    NSLog(@"Mission: %@, Name = %@, Available = %@", key, missionName, available);
}];

Note that you won't necessarily get the missions back in the same order as they are defined in the dictionary. Therefore you also can't get the index. If you need to know the index, you need to change the Missions object to an array instead in the plist.

Also note that Available is defined as boolean in your plist, therefore you should store the value in an NSNumber.

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