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]