gpt4 book ai didi

ios - NSBundle pathForResource 不适用于子目录本地化?

转载 作者:行者123 更新时间:2023-11-29 12:33:54 26 4
gpt4 key购买 nike

我看到很多关于 [NSBundle MainBundle] pathForResource 的问题(是否使用 inDirectory),但我的情况似乎有所不同。

我的问题是:是的,如果没有本地化,它对子目录中的任何文件都可以正常工作。例如,它返回文件 data/a/words.txt 的正确路径当我使用

[[NSBundle mainBundle] pathForResource:@"words.txt" ofType:nil inDirectory:@"data/a"]

但是,在我将 words.txt 本地化之后,假设真实路径变为:data/a/en.lproj/words.txt , 那么上面的代码就找不到路径了。

我检查了.app包里的文件,文件已经复制到正确的路径(data/a/en.lproj) ,只是代码无法找到它。

我正在使用 XCode 5.1.1

不是 pathForResource应该自动查找文本?

最佳答案

尝试将 forLocalization 添加到 pathForResource,如下所示:

NSArray* availableLocalizations = [[NSBundle mainBundle] localizations];
NSArray* userPrefered = [NSBundle preferredLocalizationsFromArray:availableLocalizations forPreferences:[NSLocale preferredLanguages]];

NSString *indexPath = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"txt" inDirectory:@"data/a" forLocalization:[userPrefered objectAtIndex:0]];

关于ios - NSBundle pathForResource 不适用于子目录本地化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26879125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com