Home > OS >  .onChange() - Cannot convert value of type 'Double' to expected argument type '()
.onChange() - Cannot convert value of type 'Double' to expected argument type '()

Time:07-17

I'm trying to detect a change on my @State variables to update some text but I cannot get it to work because the .onChange modifier is throwing this error:

Cannot convert value of type 'Double' to expected argument type '()'

struct CalcView: View {
    @State private var width = 0.0

    var body: some View {
        VStack {
            Button(action: {
                width  = 1.0
            }, label: {
                Text("Increment width")
            })
        }
        .onChange(of: width) { // This line specifically 
            print("Width Changed")
        }
    }
}

CodePudding user response:

The closure you’re calling with the onChange modifier doesn’t take any arguments, but onChange wants to pass a Double to that closure. Try changing .onChange(of: width) { to this:

.onChange(of: width) { newWidth in

  • Related