Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

swift2

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

like image 377
Zohra Khan Avatar asked Dec 10 '22 15:12

Zohra Khan


1 Answers

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])
like image 118
Leo Dabus Avatar answered Jan 11 '23 23:01

Leo Dabus