从 Swift 中的路径获取 Bundle 引用
Get Bundle reference from Path in Swift
我想从 swift 中的路径获取 NSBundle 引用。
喜欢 Objective-c >
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
我们如何在 swift 中实现同样的目标。
这样你就可以在 swift:
let languageBundle = NSBundle(path: path)
试试这个
var path = NSBundle.mainBundle()//path until your project name.app
var pathForFile = NSBundle.mainBundle().pathForResource("Filename", ofType: "db")// path of your file in bundle
Swift 4:
var pathForFile = Bundle.main.path(forResource: "Filename", ofType: "db")
对于 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 {}
我想从 swift 中的路径获取 NSBundle 引用。
喜欢 Objective-c >
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
我们如何在 swift 中实现同样的目标。
这样你就可以在 swift:
let languageBundle = NSBundle(path: path)
试试这个
var path = NSBundle.mainBundle()//path until your project name.app
var pathForFile = NSBundle.mainBundle().pathForResource("Filename", ofType: "db")// path of your file in bundle
Swift 4:
var pathForFile = Bundle.main.path(forResource: "Filename", ofType: "db")
对于 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 {}