你能解释一下这个 Swift 代码吗
如果我为“a”键分配 nil 值,然后使用 if let 语句,这个 nil 值将被解包为 nil 并可以打印
import Foundation
var dictionary = ["a": nil, "b": "Costam", "c": nil]
dictionary.updateValue(nil, forKey: "a")
if let value = dictionary["b"] {
print("Some value: ", value)
}
print(dictionary.keys)
为了防止这种行为,我需要向 String 添加类型转换?
import Foundation
var dictionary = ["a": nil, "b": "Costam", "c": nil]
dictionary.updateValue(nil, forKey: "a")
if let value = dictionary["b"] as? String {
print("Some value: ", value)
}
print(dictionary.keys)