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   w ww  .j  a  v  a  2s  . 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