如何获取主包中子文件夹的路径? [英] How to get path to a subfolder in main bundle?
本文介绍了如何获取主包中子文件夹的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个项目,我要从 Obj-C 迁移到 Swift 3.0(我是 Swift 的菜鸟).
I have a project which I am migrating from Obj-C to Swift 3.0 (and I am quite a noob in Swift).
如何翻译这一行?
NSString *folder = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myfolder"];
我设法获得了资源路径:
I managed to get resource path:
let resoursePath = Bundle.main.resoursePath;
但是如何获取名为myfolder"的子文件夹的路径?我需要获取子文件夹的路径,而不是其中文件的路径.
But how do I get path to a subfolder named "myfolder"? I need to get a path the subfolder, not path to the files inside it.
推荐答案
在Swift-3中制作URL
,并调用appendingPathComponent
:
In Swift-3 make URL
, and call 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
方法的信息,请参阅此问答.
See this Q&A on information on stringByAppendingPathComponent
method in Swift.
这篇关于如何获取主包中子文件夹的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文