Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: How to load a Bundle from a static method

Tags:

swift

Normally, it's pretty easy to load a Bundle for a class from an instance method:

class SomeClass
    func foo() {
        let bundle = Bundle(for: type(of: self))
        // ...
    }
}

But what if we're in a static method?

class SomeClass
    static func foo() {
        let bundle = Bundle(for: ???)

    }
}

I've tried a bunch of stuff like SomeClass.type, etc, but haven't figured it out.

Also, I'm hoping to use one of the other initializers such as URL or identifier since that's fragile.

Thanks.

like image 259
SuperDuperTango Avatar asked Sep 15 '17 18:09

SuperDuperTango


2 Answers

Swift 5

static func foo() {
        let bundle = Bundle(for: Self.self)
   }
like image 106
Oleh Kudinov Avatar answered Oct 11 '22 22:10

Oleh Kudinov


You can use ClassName.self.

class SomeClass
    static func foo() {
        let bundle = Bundle(for: SomeClass.self)

    }
}
like image 36
MJN Avatar answered Oct 11 '22 22:10

MJN