#20: Dictionary with Nil Values
Question
// Swift Quiz
— objc.io (@objcio) July 3, 2019
var dictWithNils: [String: Int?] = [
"one": 1,
"two": 2,
"none": nil
]
dictWithNils["two"] = nil
dictWithNils["none"] = nil
let result = dictWithNils.count
// What’s the result?
Solution
Solution: 1
— objc.io (@objcio) July 4, 2019
Setting a dictionary value to nil through the subscript always removes the value from the dictionary.
If we want to have a nil value in a dictionary with optional values, we have to use .some(nil).https://t.co/s7500CWmAg