NSBundle returns nil

佐手、 提交于 2019-12-11 12:14:11

问题


I wanted to read version information of my application from the plit file.

For this I am using a code as shown below.

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

        // Format output

        NSLog(@"%@",myFilePath);

The output

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

It always returns nil even if I tried to Import an existing file of type, text.txt still return nil, where text.txt is imported in the application.

I have goggled for the problem dont every one recommends to use NSBundel to read an pre imported file, but no use.

Any help, or an better way to read application version.


回答1:


Got the solution via another link in the stack overflow here.

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


        NSLog(@"%@",version);



回答2:


The reason this doesn't work is because pathForResource looks for stuff inside "MyApp.app/Contents/Resources". The Info.plist file does not reside inside the resources folder, so it's going to return nil if you look for it there. The correct way to get at it is to use the "infoDictionary" method on NSBundle.



来源:https://stackoverflow.com/questions/3608683/nsbundle-returns-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!