我想在swift中从Path获取NSBundle引用。
和Objective-c中一样>
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
如何在swift中实现相同的功能
试试这个
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")
这样你就可以在swift:
let languageBundle = NSBundle(path: path)
For 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 {}