简体   繁体   English

从.plist文件中读取

[英]read from .plist file

Got a little problem. 有点问题。 I have a .plist file which contain next data 我有一个包含下一个数据的.plist文件 http://imageshack.com/a/img856/1421/xvzm.png

So i cant understand, how i need read first array, than in array read dictionary. 所以我无法理解,我需要读取第一个数组,而不是数组读取字典。 Or maybe need rewrite file and change Root key to type dictionary, and read like this: 或者可能需要重写文件并将Root键更改为类型字典,并按如下方式读取:

NSString *errorDesc = nil;
NSPropertyListFormat format;
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
   NSUserDomainMask, YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
    plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
    propertyListFromData:plistXML
    mutabilityOption:NSPropertyListMutableContainersAndLeaves
    format:&format
    errorDescription:&errorDesc];
if (!temp) {
    NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
self.personName = [temp objectForKey:@"Name"];
self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];

You can do it by the following code: 您可以通过以下代码执行此操作:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSArray  *dataArray = [NSArray arrayWithContentsOfFile:plistPath];

This array will contain all the dictionaries, you can get them like: 这个数组将包含所有字典,你可以得到它们:

NSDictionary *dict = [dataArray objectAtIndex:0];
SString *plistFile = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistFile];

for(NSDictionary *dictionary in array) {
    NSLog(@"%@", dictionary);
}

NSDictionary *item0 = array[0];
NSString *imageName = item[0][@"name"];

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

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