我在父类中声明了一个属性:
var textLabel: UILabel! { get }
是否可以让它在子类中可写?
最佳答案
是的,可以让它在子类中可写。但是,由于它是一个计算属性,您很可能必须添加另一个存储属性来保存您正在分配的新值。下面我用字符串来说明:
class Parent {
var text: String {
get {
return "Parent"
}
}
}
class Child: Parent {
var _text: String = "Child"
override var text: String {
get {
return _text
}
set {
self._text = newValue
}
}
}
let child = Child()
child.text = "New String"
关于swift - 在 swift 中覆盖 get-only 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31997240/