[英]Overriding didSet
I want to override the property observer and calling the "super.didSet". 我想覆盖属性观察者并调用“super.didSet”。 Is that possible? 那可能吗?
class Foo {
var name: String = "" { didSet { print("name has been set") } }
}
class Bar: Foo {
override var name: String = "" {
didSet {
print("print this first")
// print the line set in the superclass
}
}
}
I have tried your code in a playground and got and error 我已经在游乐场尝试了你的代码并得到了错误
Variable with getter/ setter cannot have and initial value. 具有getter / setter的变量不能具有初始值。
So I just removed =""
from the orverriding variable. 所以我只是从orverriding变量中删除了=""
。 like below: 如下:
class Foo {
var name: String = "" { didSet { print("name has been set") } }
}
class Bar: Foo {
override var name: String {
didSet {
print("print this first")
// print the line set in the superclass
}
}
}
let bar = Bar()
bar.name = "name"
and that's what I got in consol: 这就是我在康复中所得到的:
name has been set
print this first
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.