NSBundle的使用,注意mainBundle和Custom Bundle的區別


1、[NSBundle mainBundle],文件夾其實是Group,如左側的樹形文件管理器

Build之后,文件直接就復制到了根目錄下,於是讀取的方法,應該是這樣:

NSString *earth = [[NSBundle mainBundle] pathForResource:@"Brad Cox" ofType:@"png"];

2、使用文件夾的時候,Build結果

依然使用同樣的方法,不需要制定文件夾路徑

//inDirectory參數可有可無
NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png" inDirectory:@"Images/MoreImages"]; 
NSString *earth = [[NSBundle mainBundle] pathForResource:@"Matthijs" ofType:@"png"];

 

編譯之后,mainBundle的資源都是放到RootFolder下,所以,可以直接訪問,不要指定內部路徑

 

3、使用其他的Bundle

 

NSString *resourceBundle = [[NSBundle mainBundle] pathForResource:@"Resources" ofType:@"bundle"];
    NSLog(@"resourceBundle: %@", resourceBundle);
    NSString *earth = [[NSBundlebundleWithPath:resourceBundle] pathForResource:@"Matthijs"ofType:@"jpg"inDirectory:@"Images/MoreImages"];

    NSLog(@"path: %@", earth);   

使用Custom bundle,訪問內部的子文件夾,需要指定inDirectory參數,這個mainBundle不同,這是為何?

注意:Since
bundles other than the main bundle can have folders embedded inside them, to access
files inside folders of a bundle other than the main bundle it is best to use the pathFor
Resource:ofType:inDirectory: method of NSBundle to explicitly specify the folder in
which a specific file/resource exists.

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM