Detecting when state variable changes
I tried using willSet
or didSet
on an @State
variable, but it didn’t work
@State var consumed: Bool = false {
didSet {
print("never called")
}
}
Instead what works is to use the onChange
event:
Toggle("My toggle", isOn: $consumed)
.onChange(of: self.consumed) { v in
print("is called")
}