Swift equivalent of [NSBundle bundleForClass:[self class]]

What is swift equivalent of next code:

[NSBundle bundleForClass:[self class]]

I need load resources from test bundle (JSON data)


Solution 1:

Never used, but I think it should be this:

Swift <= 2.x

NSBundle(forClass: self.dynamicType)

Swift 3.x

Bundle(for: type(of: self))

Solution 2:

Swift 3:

Bundle(for: type(of: self))

Solution 3:

Swift 5

Bundle(for: Self.self)

Solution 4:

I personally like:

let bun = NSBundle(forClass: self.classForCoder)

Solution 5:

let bundle = NSBundle(forClass:object_getClass(self))