[英]I can read from a plist but fail to read from another one
我在“支持文件”中创建了两个名为info.plist和data.plist的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];
并尝试显示plist中的数据:
NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSLog(@"%@", data1);
我从information.plist获取数据,但从data.plist获取“空”。
我错过了什么?
感谢您的帮助!
尝试这个:
// 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];
说明:您的plists在应用程序捆绑包中,而不在Documents目录中,这就是为什么您没有得到它们的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.