xcode NSBundle pathForResource 返回 nil 和 subdirs

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5022544/
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-09-09 03:42:27  来源:igfitidea点击:

NSBundle pathForResource returns nil with subdirs

xcodeios-simulatornsbundle

提问by Martin Wickman

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":

我的应用程序中有一堆目录和文件,例如images/misc/mainmenu_background.. 我在“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];

After this call, imagescontains the path /Users/wic/Library/Application Support/iPhone Simulator/3.2/Applications/8F67150B-71E6-4735-8CC6-38B3CE6D3568/Foo.app/images.

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

But images_miscand images_misc_fileare nil. Double-checking my file system to check if the file is there:

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

$ 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

Apparently the file is there.

显然文件在那里。

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

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

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

我的设置有问题,还是NSBundleiPad 3.2 中的这种行为是正确的?不幸的是,我没有实际的物理 iPad 来测试它。

回答by Lily Ballard

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

如果您需要访问目录中的文件,则应-[NSBundle pathForResource:ofType:inDirectory:]改为使用。所以你的代码应该看起来像

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

回答by mt_

Even though this has been answered already, I would like to add that -[NSBundle pathForResource:ofType:inDirectory:]has different case-sensitivity depending whether it is iPhone simulator or iPad simulator or the device. For example, iPhone Simulator 4.0 it seems to be case insensitive, while on iPad Simulator 3.2 and the device - case sensitive. Thus files that are found on iPhone 4.0 simulator might not be found on IPad Simulator 3.2 or the device if the cases don't match.

尽管这已经得到了回答,但我想补充一点,-[NSBundle pathForResource:ofType:inDirectory:]根据是 iPhone 模拟器、iPad 模拟器还是设备,它具有不同的大小写敏感性。例如,iPhone Simulator 4.0 似乎不区分大小写,而在 iPad Simulator 3.2 和设备上 - 区分大小写。因此,如果外壳不匹配,在 iPhone 4.0 模拟器上找到的文件可能在 iPad Simulator 3.2 或设备上找不到。