How to convert the string to int in Swift
In this tutorial, we are going to learn about how to convert a string to int(integer) in Swift.
Converting string to int
To convert a string to an int, we can use the built-in
Int() initializer syntax by passing the string as an argument to it.
In this example, we are converting the string
"929" to an int
let str = "929" let convertedNumber = Int(str) ?? 0 print(convertedNumber)
In the above code, we have added the
Int(str) ?? 0. it means the
Int() returns an optional value
0; if it fails to convert the string into an int.