如何获取主包中子文件夹的路径?
问题描述:
我有一个项目,我正在从Obj-C迁移到Swift 3.0(而且我在Swift中是个不错的选择)。如何获取主包中子文件夹的路径?
如何翻译此行?
NSString *folder = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myfolder"];
我设法得到资源路径:
let resoursePath = Bundle.main.resoursePath;
但我怎么路径名为 “MyFolder文件” 子文件夹? 我需要获取子文件夹的路径,而不是其中的文件路径。
答
在斯威夫特-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!) 。
答
您可以使用此方法来获取捆绑子目录的上市并获得唯一的特定类型的资源:
Bundle.main.paths(forResourcesOfType: type, inDirectory: folder)
使用'Bundle.main.resourceURL'将其简化为... –
@MartinR谢谢评论! – dasblinkenlight
这是最好的答案。 –