【发布时间】:2015-07-01 15:25:24
【问题描述】:
我希望能够从 Nib 中提取 UIView 子类的实例。
我希望能够调用 MyCustomView.instantiateFromNib() 并拥有一个 MyCustomView 实例。我几乎准备好通过桥接头移植我拥有的工作 Objective-C 代码,但我想我会先尝试惯用的方法。那是两个小时前的事了。
extension UIView {
class func instantiateFromNib() -> Self? {
let topLevelObjects = NSBundle.mainBundle().loadNibNamed("CustomViews", owner: nil, options: nil)
for topLevelObject in topLevelObjects {
if (topLevelObject is self) {
return topLevelObject
}
}
return nil
}
}
现在if (topLevelObject is self) { 是错误的,因为“'is' 之后的预期类型”。之后我的尝试显示了很多关于我对 Swift 类型系统的不了解的地方。
if (topLevelObject is Self) {if (topLevelObject is self.dynamicType) {if (topLevelObject is self.self) {- 一百万个其他变体是not even wrong。
感谢任何见解。
【问题讨论】:
标签: swift