簡體   English   中英

pathForResource ofType返回nil

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM