Tsallaka zuwa abun ciki
Justice Tech School

Type Casting

as? safely casts, as! forces and is tests a type.

Example
let items: [Any] = [1, "two", 3.0]
for i in items {
    if let n = i as? Int { print("int", n) }
    else if i is String { print("string") }
    else { print("other") }
}

Exercise

Safe cast.

x ? Int

Quiz Test

What does as? give on failure?

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