ios 从 Info plist 中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9805410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:27:15  来源:igfitidea点击:

Getting data from the Info plist

objective-ciosxcodeios5

提问by Darren

Whenever I want to get data from a plist file I use the following code:

每当我想从 plist 文件中获取数据时,我都会使用以下代码:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"FILE_NAME" ofType:@"plist"];
NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:filePath]; 

But now I'm trying to read in data from the Info plist, and filePath is nil. Is there a different way to get data from the Info plist?

但是现在我试图从 Info plist 中读取数据,而 filePath 为零。有没有其他方法可以从 Info plist 中获取数据?

回答by Damo

From an earlier SOanswer of mine. Attributes from the info.plist for your project are directly accessible by the following...

来自我较早的SO答案。您项目的 info.plist 中的属性可以通过以下方式直接访问...

[[NSBundle mainBundle] objectForInfoDictionaryKey:key_name];

Your filePath is nil simply because it can't find the file - check spellings & check if the file you are trying to read from is actually in the bundle etc.

您的 filePath 为零仅仅是因为它找不到文件 - 检查拼写并检查您尝试读取的文件是否实际上在包中等。

回答by Oscar Gomez

I do not think there is another way (unless it is the info.plist file then see Damo's comment), instead I would focus on figuring out why the filePath is nil, perhaps the plist file is no longer under target>build phases>copy bundle resources?

我不认为有另一种方式(除非它是 info.plist 文件然后看到 Damo 的评论),相反我会专注于弄清楚为什么 filePath 为零,也许 plist 文件不再在目标>构建阶段>复制下捆绑资源?

回答by Amr

Replace

代替

[[NSBundle mainBundle] pathForResource:@"FILE_NAME" ofType:@"plist"]

with

[[NSBundle mainBundle] pathForResource: @"Info" ofType: @"plist"]