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)//  w  ww .  j  a va 2  s.  c  o  m

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