This is an offer from the documentation, as you can see it by design. If this does not meet your needs, then you are doomed to use updateValue(value: Value, forKey: Hashable) , which also works more predictable.
I found that when using NSMutableDictionary instead of Dictionary it works as "expected"
let ns = NSMutableDictionary(dictionary: d) let x: Any? = nil ns["foo"] = x
However, the case let x = Any? = nil let x = Any? = nil it seems that there is an error in the implementation of Swift, at least until the version of Apple Swift version 3.0.1 (swiftlang-800.0.58.6 clang-800.0.42.1)
Btw. when all elements from the Dictionary are deleted, type of Dictionary is still correctly recognized
let x: String? = nil d["foo"] = x // d is now [:] let m = Mirror(reflecting: d) // Mirror for Dictionary<String, Optional<Any>>
I allowed myself to add an error to Swift lang: https://bugs.swift.org/browse/SR-3286
sliwinski.lukas
source share