Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load a custom bundle in Swift 3

Tags:

ios

swift

swift3

In Objective-C I was doing this:

NSString *path = [[NSBundle mainBundle] pathForResource:@"LUIImages" ofType:@"bundle"];
path = [[NSBundle bundleWithPath:path] pathForResource:_imageHash ofType:nil];

But I can't seem to find equivalent in swift 3

let bundlePath: String = Bundle.main.path(forResource: "LiveUI", ofType: "bundle")!

But what is the next step? Or is there any better way to load a custom bundle?

like image 365
Ondrej Rafaj Avatar asked Sep 05 '16 09:09

Ondrej Rafaj


1 Answers

Use the Bundle(path:) constructor and avoid forced unwrapping:

if let bundlePath = Bundle.main.path(forResource: "LiveUI", ofType: "bundle"),
    let bundle = Bundle(path: bundlePath),
    let path = bundle.path(forResource: "...", ofType: nil) {
    print(path)
} else {
    print("not found")
}

An alternative is to load the bundle using the bundle identifier (defined in the bundle's Info.plist):

let bundle = Bundle(identifier: "com.company.bundle.LiveUI")
like image 134
Martin R Avatar answered Oct 04 '22 21:10

Martin R