从Swift中的Path获取Bundle引用

时间:2015-07-29 09:26:06

标签: ios swift filepath nsbundle

我想在swift中从Path获得NSBundle引用。

类似于Objective-c>

NSBundle* languageBundle = [NSBundle bundleWithPath:path];

我们如何在swift中实现同样的目标。

3 个答案:

答案 0 :(得分:10)

试试这个

 var path = NSBundle.mainBundle()//path until your project name.app

 var pathForFile = NSBundle.mainBundle().pathForResource("Filename", ofType: "db")// path of your file in bundle
斯威夫特4:

var pathForFile = Bundle.main.path(forResource: "Filename", ofType: "db")

答案 1 :(得分:3)

这样你就可以在swift中做到这一点:

let languageBundle = NSBundle(path: path)

答案 2 :(得分:3)

对于Swift 3:

let bundleFromPath = Bundle(path: path)

从捆绑包中加载文件列表:

let docPath = Bundle.main.resourcePath! + "/" + bundleName + ".bundle"
let fileManager = FileManager.default
do {
    let filesFromBundle = try fileManager.contentsOfDirectory(atPath: docPath)
    print(filesFromBundle)
} catch {}