![](/img/trans.png)
[英]-[NSBundle pathForResource:ofType:inDirectory:] returns nil for localized resources
[英]pathForResource ofType returns nil
這是我正在使用的代碼。
NSString* path = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"plist"];
if(path == nil)
{
NSLog(@"file not found");
}
當我運行它時,它打印在控制台中找不到的文件。
我的file.plist在我的可可項目的支持文件文件夾中。 mainBundle在哪里查找文件。 這讓我很難受。 我得到它尋找.app文件,但在開發應用程序時doe mainBundle看起來?
pathForResource:ofType:
僅查找Resources
文件夾(和本地化文件夾)。 如果要將文件放入捆綁包中的專用文件夾,則需要使用pathForResource:ofType:inDirectory:
並提供目錄名稱。
如果您找到的NSBundle
是您想要的:
NSBundle *yourTargetBundle = [NSBundle mainBundle];
or
NSBundle *yourTargetBundle = [NSBundle bundleWithURL:[[NSBundle mainBundle]
URLForResource:@"YourCustomBundleName" withExtension:@"bundle"]];
並獲取nil結果使用函數: pathForResource: ofType:
. 然后,您可以在yourTargetBundle
下記錄資源的paths
:
// Given that your resource type is .png and have no sub dir
NSArray *resoursePaths = [yourTargetBundle pathsForResourcesOfType:@"png" inDirectory:nil];
NSLog(@"resoursePaths == %@",resoursePaths);
它將記錄png
類型的所有資源路徑。 您可以使用image
對象:
targetImg = [[UIImage alloc] initWithContentsOfFile:@"OnePngPathYouChose"];
我認為上述方法是解決return nil
情況的一種方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.