Swift - Data Types Conversion

Introduction

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:

Demo

let three = Int("3") // 3 
print(three)/*from   ww  w. jav a  2  s .  co 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