2016-11-19 179 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) 
相关问题