-1

I'm very new to Xcode and swift so this might be very easy to answer but I have looked at different forum posts and tutorials for an hour now and haven't found anything that works. I have a TextField that I only allow numbers as input. I want to convert the Binding that I get from the TextField to an Integer. When I typ in any number and print the String via the print command it tells me that the String is a 5 for example. When I use the Int() command and print this value I get the debug value nil even tho it told me it is a number. Any idea what I could do differently or is there maybe an easier/other way?

Torke
  • 63
  • 9

1 Answers1

0

this will update your Int each time you write new number

struct ContentView: View {
@State var txt: String = ""
@State var txtvalue: Int = 0


var body: some View {
    TextField("", text: $txt)
        .keyboardType(.numberPad)
        .onChange(of: txt) { value in
            txtvalue = Int(value) ?? 0
            print(txtvalue)
        }
}
Maha Mamdouh
  • 106
  • 6