I want use willSet to control a condition, and make necessary job for setting right value, but willSet did not help to make condition happen! The value still gets bigger than 5, what am I doing wrong?
import SwiftUI
struct ContentView: View {
@State private var value: Int = Int() {
willSet(newValue) {
if newValue > 5 {
value = 5
}
}
}
var body: some View {
Text(value.description)
.padding()
Button("random update") { value = Int.random(in: 0...10) }
}
}