I am new on Swift. I am trying to convert string to character array and I want the integer value of character. Here is my code:
var string = "1234"
var temp = Array(string.characters)
var o = Int(temp[0])
But at line 3 I am getting above error. What's wrong with this code? Please help me
You need to map your Character to String because Int has no Character initializer. You can also map your Character array to String Array
var temp = string.characters.map(String.init)
or convert your character to String when initializing your var
var o = Int(String(temp[0]))
Swift 4
let string = "1234"
let temp = string.map(String.init)
let o = Int(temp[0])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With