#25: Optional Promotion
Question
// Swift Quiz
— objc.io (@objcio) August 7, 2019
let numbers: [String?] = ["1", "two", nil]
let mapped: [Int?] = numbers.compactMap { Int($0 ?? "0") }
mapped.count
// What's the value of mapped.count?
Solution
Solution: 3
— objc.io (@objcio) August 8, 2019
Sometimes implicit conversions can be confusing.
In this case, because of implicit optional conversion, the nil value gets promoted to a `.some(nil)`. That means `mapped` contains 1, nil and 0.https://t.co/s7500CWmAg