2016-11-19 49 views
2

我有一個項目,我正在從Obj-C遷移到Swift 3.0(而且我在Swift中是個不錯的選擇)。如何獲取主包中子文件夾的路徑?

如何翻譯此行?

NSString *folder = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myfolder"]; 

我設法得到資源路徑:

let resoursePath = Bundle.main.resoursePath; 

但我怎麼路徑名爲 「MyFolder文件」 子文件夾? 我需要獲取子文件夾的路徑,而不是其中的文件路徑。

回答

7

在斯威夫特-3做URL,並調用appendingPathComponent

let resourcePath = Bundle.main.resourcePath 
let subdir = URL(fileURLWithPath:resourcePath!).appendingPathComponent("sub").path 

或者乾脆

let subdir = Bundle.main.resourceURL!.appendingPathComponent("sub").path 

this Q&A信息在斯威夫特stringByAppendingPathComponent方法(感謝,Martin R!) 。

+4

使用'Bundle.main.resourceURL'將其簡化爲... –

+0

@MartinR謝謝評論! – dasblinkenlight

+0

這是最好的答案。 –

0

您可以使用此方法來獲取捆綁子目錄的上市並獲得唯一的特定類型的資源:

Bundle.main.paths(forResourcesOfType: type, inDirectory: folder) 
相關問題