我创建了一个名为“医院”的plist,将第一行作为一个数组插入,其中有三个项目作为字符串。
我的代码出了什么问题:
NSArray *hospitals = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Hospitals" ofType:@"plist"]];
NSString *first = [[NSString alloc] initWithFormat:@"%@", [hospitals objectAtIndex:0]];
NSLog(@"%@",first);这将导致我(null)的结果!
发布于 2011-08-28 18:51:20
属性列表文件不存在(检查目标的"Copy Resources“构建阶段),有不同的名称(请注意,iOS的文件系统区分大小写),根本不是属性列表(可能有语法错误),不可读,或者属性列表的根不是数组,而是字典。
如果为hospitals == nil,则上述任一项都是问题所在。
发布于 2011-08-28 18:47:37
试试这个:
NSString *errorDesc = nil;
NSPropertyListFormat format;
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:[[NSBundle mainBundle] pathForResource:@"Hospitals" ofType:@"plist"]];
NSMutableDictionary *properties = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
NSArray *hospitals = (NSArray *)[properties valueForKey:@"names"];
NSString *first = [hospitals objectAtIndex:0];
NSLog(@"%@",first);https://stackoverflow.com/questions/7220392
复制相似问题