繁体   English   中英

无法从mainbundle中读取plist

[英]Can not read plist from mainbundle

我遇到一些奇怪的问题。

我无法从应用程序mainbundle中读取plist文件

该文件位于“副本捆绑资源”中

但以某种方式无法读取

这是代码

NSLog(@"%@",[[NSBundle mainBundle] pathForResource:@"8chk" ofType:@"plist"]);

它一直打印(null)

这是屏幕截图

在此处输入图片说明

谁能告诉我发生了什么事?

恐怕要说的是,您没有将此8chk.plist添加到目标中。

再次将plist文件拖放到项目中,确保在将文件添加到项目中时说“复制ProjectName中的文件”时选中复选框

然后使用此代码使其可访问并从plist读取值

NSString *path = [[NSBundle mainBundle] pathForResource: @"YourPLIST" ofType: @"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: path];
[id obj = [dict objectForKey: @"YourKey"]; 

要么

// Reads the value of the custom key you added to the Info.plist
NSString *value = [mainBundle objectForInfoDictionaryKey:@"key"];

//Log the value
NSLog(@"Value = %@", value);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Info.plist"];

这可能有效。 确保将plist复制到目标并放在根目录中。也可以尝试将plist文件放在应用默认plist保留的位置。

:)

嗨Shoeb Amin,请检查您的plist文件是否已在Target-> Copy Bundle资源中添加

您的路线是错误的。 如果项目导航器显示正确的树,则copy bundle resource应仅读取copy bundle resource 8chk.plist而不是8chk.plist ..in resources/8chk

暂无
暂无

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

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