简体   繁体   English

无法从mainbundle中读取plist

[英]Can not read plist from mainbundle

I am having some strange problem. 我遇到一些奇怪的问题。

I cant read plist file from application mainbundle 我无法从应用程序mainbundle中读取plist文件

the file is in the "copy bundle resource" 该文件位于“副本捆绑资源”中

but somehow it can not be read 但以某种方式无法读取

here is the code 这是代码

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

it prints (null) all the time 它一直打印(null)

here is the screenshot 这是屏幕截图

在此处输入图片说明

can anyone tell me what is going on? 谁能告诉我发生了什么事?

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

Drag and drop plist file again in project, make sure you tick on checkbox while adding file to project which is saying "copy file in ProjectName" 再次将plist文件拖放到项目中,确保在将文件添加到项目中时说“复制ProjectName中的文件”时选中复选框

then use this code to make it accessible and read values from plist 然后使用此代码使其可访问并从plist读取值

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

OR 要么

// 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"];

This might work. 这可能有效。 make sure to copy the plist to target and put it in root directory.also u can try to put the plist files where the apps default plist is kept. 确保将plist复制到目标并放在根目录中。也可以尝试将plist文件放在应用默认plist保留的位置。

:) :)

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

Your path is wrong. 您的路线是错误的。 copy bundle resource should read just 8chk.plist , not 8chk.plist ..in resources/8chk if the project navigator shows the correct tree. 如果项目导航器显示正确的树,则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