Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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")
        }
    }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading