What is the quick equivalent of the following code:
[NSBundle bundleForClass:[self class]]
I need download resources from a test suite (JSON data)
Never used, but I think it should be like this:
Swift <= 2.x
NSBundle(forClass: self.dynamicType)
Swift 3.x
Bundle(for: type(of: self))
Swift 3:
I personally like:
let bun = NSBundle(forClass: self.classForCoder)
let bundle = NSBundle(forClass:object_getClass(self))
The selected answer did not work for me in a static subclass of UIView, but I found this:
Bundle(for: self.classForCoder)
This also works when you want to get a Bundle within a test target.
Bundle
Download xib for dynamicType class
let bundle = NSBundle(forClass: self.dynamicType) let nib = UINib(nibName: "CellForAlert", bundle: bundle) let view = nib.instantiateWithOwner(self, options: nil).first as! UIView view.frame = bounds view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight] self.addSubview(view);
If you work in a classroom, then
Sometimes you can work in a structure, then you need to use any class included
Bundle(for: AnyClassInTheBundle.self)
In Swift 3.0 you can use:
func kZWGetBundle() -> Bundle{ return Bundle(for: AnyClass.self as! AnyClass) }