NSBundle pathForResource 返回 nil 和子目录
我的应用程序中有一堆目录和文件,例如 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];
在此调用之后,images
包含路径 /Users/wic/Library/Application Support/iPhone Simulator/3.2/Applications/ 8F67150B-71E6-4735-8CC6-38B3CE6D3568/Foo.app/images
。
但是images_misc
和images_misc_file
是nil
。仔细检查我的文件系统以检查该文件是否在那里:
$ 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”或任何其他模拟器版本,一切都会按预期进行。
我的设置是否有问题,或者 iPad 3.2 中 NSBundle
的行为是否正确?不幸的是,我没有实际的 iPad 来测试它。
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];
After this call, images
contains the path /Users/wic/Library/Application Support/iPhone Simulator/3.2/Applications/8F67150B-71E6-4735-8CC6-38B3CE6D3568/Foo.app/images
.
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
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您需要访问目录中的文件,则应该使用
-[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尽管这个问题已经得到了回答,但我想补充一点,
-[NSBundle pathForResource:ofType:inDirectory:]
具有不同的区分大小写,具体取决于它是 iPhone 模拟器还是 iPad 模拟器或设备。例如,iPhone Simulator 4.0 似乎不区分大小写,而在 iPad Simulator 3.2 和设备上则区分大小写。因此,如果大小写不匹配,在 iPhone 4.0 模拟器上找到的文件可能无法在 iPad Simulator 3.2 或设备上找到。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.