Fast equivalent [NSBundle bundleForClass: [native class]]

What is the quick equivalent of the following code:

[NSBundle bundleForClass:[self class]] 

I need download resources from a test suite (JSON data)

+112
swift
Sep 03 '14 at 18:39
source share
8 answers

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)) 
+197
Sep 03 '14 at 18:42
source share

Swift 3:

 Bundle(for: type(of: self)) 
+32
Sep 22 '16 at 9:07
source share

I personally like:

 let bun = NSBundle(forClass: self.classForCoder) 
+12
Jan 27 '16 at 10:11
source share
 let bundle = NSBundle(forClass:object_getClass(self)) 
+9
Oct 27 '14 at 18:26
source share

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.

+6
May 30 '17 at 17:50
source share

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); 
+3
Apr 20 '16 at 10:10
source share

If you work in a classroom, then

 Bundle(for: type(of: self)) 

Sometimes you can work in a structure, then you need to use any class included

 Bundle(for: AnyClassInTheBundle.self) 
+1
Nov 21 '18 at 3:31
source share

In Swift 3.0 you can use:

 func kZWGetBundle() -> Bundle{ return Bundle(for: AnyClass.self as! AnyClass) } 
0
Dec 12 '16 at 2:33
source share



All Articles