下面是一个例子项目:http://cl.ly/3N2u2i1S441M为什么我的超类在Swift中调用子类的方法而不是自己的方法?
我在UITableViewCell
超,因为正在启动子当我打电话super.init()
。在子类和超类的init
的底部,我调用一个方法,调用styleCell
,将样式应用于它。这个方法来自它们都符合的协议,其中一个隐式地符合,因为它是子类,它覆盖了方法。
在超类的终结init
,这种风格方法被调用,但它调用子类styleCell
方法,而不是自己的。
为什么地球上会发生这种情况?
这是Swift的错误吗?我附加了一些代码,除了上述项目,以显示该问题:
超类的表格单元格:
class SuperTableViewCell: UITableViewCell, Style {
var mysuperview: UIView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
mysuperview = UIView()
doStyle()
}
required init?(coder aDecoder: NSCoder) {
fatalError("Must be created in code.")
}
func doStyle() {
print("super class")
}
}
子类的表格单元格:
class SubTableViewCell: SuperTableViewCell {
var mysubview: UIView!
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
mysubview = UIView()
doStyle()
}
required init?(coder aDecoder: NSCoder) {
fatalError("Must be created in code.")
}
override func doStyle() {
super.doStyle()
mysubview!.backgroundColor = UIColor.greenColor()
}
}
Style类和协议:
class StyleManager: NSObject {
}
protocol Style {
func doStyle()
}
这会导致发生运行时错误,并在子类单元尝试设置它时发生崩溃在doStyle()
查看。
你可以发布代码,MCVE吗? –
MCVE是什么意思?我会尽快获得代码。 –
[MCVE](http://stackoverflow.com/help/mcve) –