TextField with Double or Int not binding back ?

Hi there,

In a SwiftUI View, I have a

@State var test: Double?

and a TextField like that :

TextField("Test", value: $test, formatter: NumberFormatter())

the variable test is constantly nil and never being populated with the Double value of the entered text.

Is it a bug or a concept error of mine ??

Thanks!

@State properties require you to give them an initial value.

@State var test: Double? = 0.0

I'm facing the same issue on the iOS 17.2 simulator with TextField.init(_:value:format:prompt:). Even though the official documentation provides an example binding to a Double?, every time I submit the text field, the state variable is reset back to nil.

https://developer.apple.com/documentation/swiftui/textfield/init(_:value:format:prompt:)-6ug7k

TextField with Double or Int not binding back ?
 
 
Q