NSBundle 返回零

Posted

技术标签:

【中文标题】NSBundle 返回零【英文标题】:NSBundle returns nil 【发布时间】:2010-08-31 11:50:25 【问题描述】:

我想从 plit 文件中读取我的应用程序的版本信息。

为此,我使用如下所示的代码。

NSString *myFilePath = [[NSBundle mainBundle]
                                pathForResource:@"MyApp-Info"
                                ofType:@"plist"];

        // Format output

        NSLog(@"%@",myFilePath);

输出

 2010-08-31 17:14:10.095 MyApp[8759:20b] (null)

即使我尝试导入现有类型的文件,它总是返回 nil,text.txt 仍然返回 nil,其中 text.txt 是在应用程序中导入的。

我已经看到了这个问题,不是每个人都建议使用 NSBundel 来读取预先导入的文件,但没有用。

任何帮助,或阅读应用程序版本的更好方法。

【问题讨论】:

【参考方案1】:

通过stack overflow here 中的另一个链接获得解决方案。

NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];


        NSLog(@"%@",version);

【讨论】:

【参考方案2】:

这不起作用的原因是 pathForResource 在“MyApp.app/Contents/Resources”中查找内容。 Info.plist 文件不位于资源文件夹中,因此如果您在那里查找它,它将返回 nil。正确的方法是在 NSBundle 上使用“infoDictionary”方法。

【讨论】:

你能把代码放在这里,这样它可以帮助那些正在寻找某种相同问题的人。

以上是关于NSBundle 返回零的主要内容,如果未能解决你的问题,请参考以下文章

更新 Cocoapods gem 后,测试中的类似乎是从不同的 NSBundle 加载的,导致零单例

NSBundle 返回每个系统框架

使用 OCUnit 进行单元测试时的 NSBundle 返回(空)

NSBundle.mainBundle() 返回 nil

来自 NSBundle 路径的 NSURL 返回 nil

NSBundle 返回 NULL