swift等价于]
下一个代码的快速等价物是什么:
[NSBundle bundleForClass:[self class]]
我需要从testing包(JSON数据)加载资源
从来没有使用过,但我认为这应该是这样的:
Swift <= 2.x
NSBundle(forClass: self.dynamicType)
Swift 3.x
Bundle(for: type(of: self))
Swift 3:
Bundle(for: type(of: self))
我个人喜欢:
let bun = NSBundle(forClass: self.classForCoder)
let bundle = NSBundle(forClass:object_getClass(self))
加载类的dynamicType的xib
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);
在Swift 3.0中,你可以使用:
func kZWGetBundle() -> Bundle{ return Bundle(for: AnyClass.self as! AnyClass) }
所选答案不适用于我在UIView子类的静态方法,但我发现这一点:
Bundle(for: self.classForCoder())