[英]I can read from a plist but fail to read from another one
I created two plist file named information.plist and data.plist in my Supporting Files, and I add both them to Copy Bundle Resource. 我在“支持文件”中创建了两个名为info.plist和data.plist的plist文件,并将它们都添加到“复制捆绑包资源”中。
I use the code below to read data from the plist. 我使用下面的代码从plist读取数据。
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory , NSUserDomainMask , YES );
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSMutableDictionary* dictPlist = [ [ NSMutableDictionary alloc ] initWithContentsOfFile:filePath];
and try to show data from the plist: 并尝试显示plist中的数据:
NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSLog(@"%@", data1);
I get the data from information.plist but get "null" from data.plist. 我从information.plist获取数据,但从data.plist获取“空”。
Did I miss something? 我错过了什么?
Thanks for your helping! 感谢您的帮助!
Try this: 尝试这个:
// Path of plist from bundle
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
// Since your plist's root is a dictionary else you should form NSArray from contents of plist
NSDictionary *dataPlist = [NSDictionary dictionaryWithContentsOfFile:plistPath];
Explanation : Your plists are in application bundle and not inside Documents directory which is why you are not getting them. 说明:您的plists在应用程序捆绑包中,而不在Documents目录中,这就是为什么您没有得到它们的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.