NSBundle返回nil

时间:2010-08-31 11:50:26

标签: iphone objective-c nsbundle

我想从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来读取预先导入的文件,但没有用。

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

2 个答案:

答案 0 :(得分:2)

通过stack overflow here中的其他链接获得解决方案。

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


        NSLog(@"%@",version);

答案 1 :(得分:2)

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