有点问题。我有一个.plist文件,其中包含下一个数据
所以我无法理解,我需要读取第一个数组,而不是数组读取字典。或者可能需要重写文件并将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"]];
答案 0 :(得分:1)
您可以通过以下代码执行此操作:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSArray *dataArray = [NSArray arrayWithContentsOfFile:plistPath];
这个数组将包含所有词典,你可以得到它们:
NSDictionary *dict = [dataArray objectAtIndex:0];
答案 1 :(得分:1)
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"];