Get Bundle reference from Path in Swift

16,956

Solution 1

This way you can do that in swift:

let languageBundle = NSBundle(path: path)

Solution 2

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")

Solution 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 {}
Share:
16,956
Alok
Author by

Alok

Updated on June 20, 2022

Comments

  • Alok
    Alok almost 2 years

    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 .

  • Alok
    Alok almost 9 years
    but its returning nil for me .
  • Alok
    Alok almost 9 years
    i want to fetch one string file from bundle for localization purpose .
  • Dharmesh Kheni
    Dharmesh Kheni almost 9 years