TextField gives error message

It is driving me crazy, because I really don't know what I am doing wrong. I have a observable class:

@Observable class Variables: Identifiable { var id: UUID() var xValue: Int = 1 var yValue: Int = 1 }

And a main view that calls a Subview to set the variables, using environment to set variables to the Subview, because this is a very simplified code example and in the final code lot more subview are gonna use this data.

struct MainView: View {

@State var vars = Variables()

var body: Some View {

      VStack {

             Subview()

                 .environment(vars)
                 .padding()
             Text("Value X = \($vars.xValue)")
             Text("Value Y = \($vars.yValue)")
      }

}

struct Subview: View {

@Environment(Variables.self) private var vars

var body: Some View {

       VStack {
          TextField("xValue", value $vars.xValue, format: .number) {
                Text("X")
                }
          TextField("yValue", value $vars.yValue, format: .number) {
                Text("Y")
                }
       }

}

I Get this error for the TextFields:

Cannot convert value of type 'Int' to expected argument type 'Binding<V>'

I just don't get it. Am I mixing up different kind of bindings, is something wrong in the definition of TextField..........................................

Please help me.

Answered by SilkSound in 785946022

I actually did type it in directly and wasn't much bothered by the accuracy of it. But I missed the @Binding part of the code. I assumed the variable being an environmental value is automatically bound. Apparently not. I know it was a stupid question, but I coded a fair bit of cocoa interfaces and am still struggling to get an understanding of SwiftUI.

So, thank you very much for your help

Is it the real code or did you type it here ?

I noticed several errors:

var id: UUID()

soudé be

var id = UUID()
var body: Some View {

should be

var body: some View {

What is this closure after TextField (it is only supported in MacOS: https://stackoverflow.com/questions/58776561/add-label-to-swiftui-textfield):

            TextField("xValue", value: $vars.xValue, format: .number) {
                Text("X")
            }

So here is a code that works and that is properly formatted with code formatter.

If that's OK, please close the thread by marking this answer as correct. Otherwise explain what's the problem.

@Observable class Variables: Identifiable {
    var id = UUID() // <<-- Changed here
    var xValue: Int = 1
    var yValue: Int = 1
}

struct MainView: View {
    @State var vars = Variables()
    
    var body: some View {
        VStack {
            Subview()
                .environment(vars)
                .padding()
            Text("Value X = \(vars.xValue)")    // No $vars here
            Text("Value Y = \(vars.yValue)")
        }
    }
}
    
struct Subview: View {
    @Environment(Variables.self) private var vars

    var body: some View {
        @Bindable var vars = vars   // <<-- Added here // https://developer.apple.com/documentation/swiftui/bindable

        VStack {
            HStack {
                Text("X")
                TextField("xValue", value: $vars.xValue, format: .number) /*{ Text("X") }*/  // <<-- Changed here
                    .textFieldStyle(.roundedBorder)
            }
            HStack {
                Text("Y")
                TextField("yValue", value: $vars.yValue, format: .number) /*{ Text("Y") }*/
                    .textFieldStyle(.roundedBorder)
            }
        }
    }
}
Accepted Answer

I actually did type it in directly and wasn't much bothered by the accuracy of it. But I missed the @Binding part of the code. I assumed the variable being an environmental value is automatically bound. Apparently not. I know it was a stupid question, but I coded a fair bit of cocoa interfaces and am still struggling to get an understanding of SwiftUI.

So, thank you very much for your help

TextField gives error message
 
 
Q