NSBundle pathForResource返回带有子目录的nil

NSBundle pathForResource返回带有子目录的nil

问题描述:

我的应用程序中有一堆目录和文件,例如images/misc/mainmenu_background..我正在"iPad Simulator 3.2"中运行以下代码:

I have a bunch of directories and files in my app, for instance images/misc/mainmenu_background.. I'm running the following code in the "iPad Simulator 3.2":

NSString *images = [[NSBundle mainBundle] pathForResource:@"images" ofType:nil];
NSString *images_misc = [[NSBundle mainBundle] pathForResource:@"images/misc" ofType:nil];
NSString *images_misc_file = [[NSBundle mainBundle] pathForResource:@"images/misc/mainmenu_background.png" ofType:nil];

此调用后,images包含路径/Users/wic/Library/Application Support/iPhone Simulator/3.2/Applications/8F67150B-71E6-4735-8CC6-38B3CE6D3568/Foo.app/images.

但是images_miscimages_misc_filenil .仔细检查我的文件系统以检查文件是否存在:

But images_misc and images_misc_file are nil. Double-checking my file system to check if the file is there:

$ ls -l "/Users/wic/Library/Application Support/iPhone Simulator/3.2/Applications/8F67150B-71E6-4735-8CC6-38B3CE6D3568/Foo.app/images/misc/mainmenu_background.png"
-rw-rw-rw-  1 wic  staff  30307 16 Feb 21:09 /Users/wic/Library/Application Support/iPhone Simulator/3.2/Applications/8F67150B-71E6-4735-8CC6-38B3CE6D3568/Foo.app/images/misc/mainmenu_background.png

显然文件在那里.

如果我切换到"iPad Simulator 4.0"或任何其他模拟器版本,一切都会按预期进行.

If I switch to "iPad Simulator 4.0", or any other simulator version for that matter everything works as expected.

我的设置是否有问题,还是iPad 3.2中NSBundle的此正确行为?不幸的是,我没有实际的物理iPad可以对其进行测试.

Is there something wrong with my setup, or is this correct behavior for NSBundle in iPad 3.2? I have no actual physical iPad to test it on unfortunately.

如果需要访问目录中的文件,则应改用-[NSBundle pathForResource:ofType:inDirectory:].因此,您的代码应改为

If you need to access a file in a directory, you should be using -[NSBundle pathForResource:ofType:inDirectory:] instead. So your code should instead look like

NSString *images_misc_file = [[NSBundle mainBundle] pathForResource:@"mainmenu_background" ofType:@"png" inDirectory:@"images/misc"];