我想覆盖属性观察者并调用"super.didSet".那可能吗?
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在游乐场尝试了你的代码并得到了错误
具有getter/setter的变量不能具有初始值.
所以我刚刚=""从orverriding变量中删除了.如下:
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"
Run Code Online (Sandbox Code Playgroud)
这就是我在consol中得到的:
name has been set
print this first
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3272 次 |
| 最近记录: |