iOS ● ● ○ Swift 6

Kodebits Day 77: Optional flatMap

Aug 17 2026
Practice optionals with a short swift challenge.

What is the output?

let nums = ["5", "x", "12"]
let first = nums.first
let val = first.flatMap(Int.init)
print(val ?? -1)
let empty: [String]? = nil
let none = empty.flatMap {
  $0.first
}
print(none ?? "N")


Try it in the online Swift Playground →

[spoiler title="Solution"]

Answer:

5
N

Explanation:

flatMap unwraps optionals and applies transforms, flattening nested optionals.

[/spoiler]


Further Reading