SwiftUI textField. String to Integer conversion

Hello everybody,
im new here and I have a problem:

i want to convert my String from "@State var number = “” " to an Integer typed in textField to use it in my function, and then display it in Text (for example: “The number is (IntFromString)”.
Could anyone help me?

Thank you:)


Hi Czylok,

Welcome to the Code Crew community.

At the risk of confusing you the simplest way to do this is to declare another State variable which is named like this:
@State var IntFromString = 0

Then you can have your function do this:

func yourFunction() {
IntFromString = Int(number)

Then in your Text that you are using to display the result you might do the following using String interpolation:
Text(“The number is: (IntFromString)”)

Hope that makes sense.

Thank you, it worked :slight_smile: