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 .

Alok
  • 24,880
  • 6
  • 40
  • 67

3 Answers3

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")
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68
3

This way you can do that in swift:

let languageBundle = NSBundle(path: path)
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
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 {}
Tim
  • 606
  • 1
  • 8
  • 19