Get Bundle reference from Path in Swift
Asked Answered
P

3

12

I want to get NSBundle reference from Path in swift.

Like in Objective-c >

NSBundle* languageBundle = [NSBundle bundleWithPath:path];

How we can achieve same in swift .

Pleasance answered 29/7, 2015 at 9:26 Comment(0)
R
3

This way you can do that in swift:

let languageBundle = NSBundle(path: path)
Richart answered 29/7, 2015 at 9:27 Comment(3)
but its returning nil for me .Pleasance
i want to fetch one string file from bundle for localization purpose .Pleasance
Check this: #30294361Richart
S
11

Try this

 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")
Sisco answered 29/7, 2015 at 9:50 Comment(0)
R
3

This way you can do that in swift:

let languageBundle = NSBundle(path: path)
Richart answered 29/7, 2015 at 9:27 Comment(3)
but its returning nil for me .Pleasance
i want to fetch one string file from bundle for localization purpose .Pleasance
Check this: #30294361Richart
C
3

For Swift 3:

let bundleFromPath = Bundle(path: path)

To load a list of files from the bundle:

let docPath = Bundle.main.resourcePath! + "/" + bundleName + ".bundle"
let fileManager = FileManager.default
do {
    let filesFromBundle = try fileManager.contentsOfDirectory(atPath: docPath)
    print(filesFromBundle)
} catch {}
Chloroprene answered 23/9, 2016 at 8:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.