NSBundle pathForResource 返回 nil 和子目录

发布于 2024-10-18 03:54:53 字数 1271 浏览 5 评论 0原文

我的应用程序中有一堆目录和文件,例如 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_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

显然该文件在那里。

如果我切换到“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若沐 2024-10-25 03:54:53

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

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

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"];
他不在意 2024-10-25 03:54:53

尽管这个问题已经得到了回答,但我想补充一点, -[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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文