The following code no longer behaves in Swift 4.2 (Xcode 10) the same it was behaving in Swift 4.1 (Xcode 9.4.1):
let key: String! = "key"
let dict: [AnyHashable:Any]? = ["key":"value"]
let val = dict?[key]
In Swift 4.1, val
receives the dictionary value ("value"), while in Swift 4.2 it's nil.
The problem goes away if I remove the Implicitly unwrapped optional (IUO), or declare the dictionary as [String:Any]
, so both
let key: String = "key"
let dict: [AnyHashable:Any]? = ["key":"value"]
let val = dict?[key]
, and
let key: String! = "key"
let dict: [String:Any]? = ["key":"value"]
let val = dict?[key]
result in val
ending up holding the string "value".
Is this an intended behaviour in Swift 4.2, or it's a compiler bug?
Asking as I have a huge codebase where both the key and the dictionary come from Objective-C code which is kinda resistant to change. So I was wondering if this change in behaviour is permanent and I should start updating the many places in code that use this pattern, or just wait until a stable build of Xcode 10 is released.