如何获得主包中子文件夹的path?
我有一个项目,我正在从Obj-C迁移到Swift 3.0(而且我在Swift中是个不错的select)。
我如何翻译这一行?
NSString *folder = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myfolder"];
我设法得到资源path:
let resoursePath = Bundle.main.resoursePath;
但是,如何获得名为“myfolder”的子文件夹? 我需要得到一个path的子文件夹,而不是path里面的文件。
在Swift-3中创buildURL
,并调用appendingPathComponent
:
let resourcePath = Bundle.main.resourcePath let subdir = URL(fileURLWithPath:resourcePath!).appendingPathComponent("sub").path
或干脆
let subdir = Bundle.main.resourceURL!.appendingPathComponent("sub").path
(谢谢, Martin R !)
有关Swift中stringByAppendingPathComponent
方法的信息,请参阅此Q&A 。
您可以使用此方法获取捆绑包子目录的列表,并仅获取特定types的资源:
Bundle.main.paths(forResourcesOfType: type, inDirectory: folder)