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.
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