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

來源:互聯網
上載者:User

標籤:

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不同,這是為何?

  

注意:Sincebundles other than the main bundle can have folders embedded inside them, to accessfiles inside folders of a bundle other than the main bundle it is best to use the pathForResource:ofType:inDirectory: method of NSBundle to explicitly specify the folder inwhich a specific file/resource exists.

  原文地址:http://www.cnblogs.com/iihe602/archive/2013/01/17/2865280.html

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.