#24: Overriding didSet
Question
// Swift Quiz
— objc.io (@objcio) July 31, 2019
class Base {
var s = "" {
didSet {
print("B: \(s)")
}
}
}
class Sub: Base {
override var s: String {
didSet {
print("S: \(s)")
}
}
}
let c = Sub()
c.s = "a"
c.s = "b"
// What is printed?
Solution
Solution: B: a, S: a, B: b, S: b
— objc.io (@objcio) August 1, 2019
Both setters get called. It’s the only sensible thing Swift could do short of disallowing an override. Not calling either one of the setters would probably be very confusing.https://t.co/s7500CWmAg