reading from plist issue
I have a plist which is an array, the file name is called startups.plist. I trie开发者_如何学God to read it into an array by the following code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"];
NSMutableArray * wordList = [[NSMutableArray alloc] initWithContentsOfFile:path];
However, wordList is always 0 here. Printed the path when testing on device, and it gives me:
/var/mobile/Applications/CCBF94D9-389C-4D63-B023-F39653FDCEF4/My.app/startups.plist
Here's what the structure looks like of the plist:
the size of the array is 0. What am I doing wrong here?
initWithContentsOfFile:
will not open plist file. Here is a sample:
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"];
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:path];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
propertyListFromData:plistXML
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&errorDesc];
NSLog(@"%@",[temp objectForKey:@"Root"]);
You have to use NSDictionary
since your plist is a Dictionary which has a key Root which is an array.
NSString *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"];
NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *wordList = (NSMutableArray *)[dict objectForKey:@"Root"];
I'm not sure about casting to NSMutableArray
. Maybe you have to do
NSMutableArray *wordList = [NSMutableArray arrayWithArray:(NSArray*)[dict objectForKey:@"Root"]]
精彩评论