Zohra Khan Zohra Khan - 10 months ago 65
Swift Question

Cannot invoke initializer for type: with an argument list of type '(_Element)'

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 map your Character array to String Array

 var temp = Array(string.characters).map{String($0)} 

or convert your character to String when initializing your var

var o = Int(String(temp[0]))