In this case we have an integer within a string. Well, it can be converted to an integer simply by using the integer initializer. Let’s look at the example code:
// Using integer's initializer. let string1 = "328" let int1 = Int(string1)
Strings can contain anything other than a number, anything, for example «Palm» instead of «328»: the Int initializer then returns an optional integer. So we better put a value. In this case we can then put this example code:
// Using integer's initializer. let string1 = "328" let int1 = Int(string1) ?? 0
You may see other ways to do this, but check that they work for you because there is deprecated code.
Here you can see what the opposite step would be like, going from Int to String.
I hope this tutorial has helped you. Do not forget to subscribe to the YouTube channel to continue learning Swift. We continue …