简体   繁体   English

从plist阅读

[英]reading from plist issue

I have a plist which is an array, the file name is called startups.plist. 我有一个plist,它是一个数组,文件名叫startups.plist。 I tried 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. 但是,这里的wordList始终为0。 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: 这是plist的结构:

在此处输入图片说明

the size of the array is 0. What am I doing wrong here? 数组的大小为0。在这里我做错了什么?

initWithContentsOfFile: will not open plist file. initWithContentsOfFile:将不会打开plist文件。 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. 您必须使用NSDictionary因为您的plist是一个Dictionary,它的键Root是一个数组。

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 . 我不确定要转换为NSMutableArray Maybe you have to do 也许你要做

NSMutableArray *wordList = [NSMutableArray arrayWithArray:(NSArray*)[dict objectForKey:@"Root"]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM