Ios 如何获取主捆绑包中子文件夹的路径?
我有一个项目,我正在从Obj-C迁移到Swift 3.0(我在Swift中是一个相当不错的noob) 我怎么翻译这句话Ios 如何获取主捆绑包中子文件夹的路径?,ios,swift3,nsbundle,Ios,Swift3,Nsbundle,我有一个项目,我正在从Obj-C迁移到Swift 3.0(我在Swift中是一个相当不错的noob) 我怎么翻译这句话 NSString *folder = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myfolder"]; 我设法获得了资源路径: let resoursePath = Bundle.main.resoursePath; 但如何获取名为“myfolder”的子文件夹的路径? 我需
NSString *folder = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"myfolder"];
我设法获得了资源路径:
let resoursePath = Bundle.main.resoursePath;
但如何获取名为“myfolder”的子文件夹的路径?
我需要获取子文件夹的路径,而不是其中文件的路径。在Swift-3中生成
URL
,然后调用appendingPathComponent
:
let resourcePath = Bundle.main.resourcePath
let subdir = URL(fileURLWithPath:resourcePath!).appendingPathComponent("sub").path
或者干脆
let subdir = Bundle.main.resourceURL!.appendingPathComponent("sub").path
(谢谢
请参见有关Swift中的
stringByAppendingPathComponent
方法的信息。您可以使用此方法获取捆绑子目录的列表,并仅获取特定类型的资源:
Bundle.main.paths(forResourcesOfType: type, inDirectory: folder)
您可以这样做:
let folderURL = resourceURL(to: "myfolder")
func resourceURL(to path: String) -> URL? {
return URL(string: path, relativeTo: Bundle.main.resourceURL)
}
@马丁纳:谢谢你的评论!这是最好的答案。非常感谢你们。