Learn Courses My Dashboard

Conversion of string to int

please how would I convert a string in textfield to a int
for example I want to use the @state variable for a calculation in my code

If you enter a numeric value into a TextField and you want to assign that to an Int variable then all you do is something like this:

Lets assume that you have defined your integer variable as:
@State private var integerVariable: Int = 0

and you have a variable for your TextField defined as:
@State private var textVariable = ""
which you bind to your TextField

To assign the textfield to an integer, do this

integerVariable = Int(textVariable)