Zohra Khan
Zohra Khan

Reputation: 5302

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

Upvotes: 4

Views: 19899

Answers (1)

Leo Dabus
Leo Dabus

Reputation: 236260

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])

Upvotes: 13

Related Questions