Swift - Data Types Conversion


Swift is a statically typed language with type inference.

You call the initializer of the type, passing in the value you want converted.

For example, to convert the string "3" to the integer 3:


let three = Int("3") // 3 
print(three)//from   w  ww.  ja v a2 s. com

let almostMeaningOfLife = String(Int(1.999999)) 

let number = Int("lorem ipsum") // nil

The outcome of last statement is nil and not a crash.

Related Topics