As The Swift Programming Language: Redirect
The willSet and didSet observers of superclass properties are called when a property is set in a subclass initializer, after the superclass initializer has been called. They aren't called while a class is setting its own properties, before the superclass initializer has been called.
Should the didSet be called on the code below?
(The code would trigger circular init and crash.)
import Combine
struct S {
var id = 0
}
class C: ObservableObject {
static let sharded = C()
@Published var index = S() {
didSet {
print("didset")
print(C.sharded.index)
}
}
var dummy: Int
init() {
// index = S(id: 1)
dummy = 2
setIndex()
}
func setIndex() {
index = S(id: 1)
print("index = 1 ed")
}
}
print(C.sharded)