Skip to content
Justice Tech School

Swift Type Conversion

Convert with initialisers: Int("42") returns an optional, String(42) always works. Casting between class types uses as, as? and as!.

Example
let a = Int("42") ?? 0
let b = String(a)
let c = Double(a) / 5
print(a, b, c)

Exercise

Number to text.

(42)

Quiz Test

Why does Int("abc") return an optional?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.