Can someone explain why this works in Swift 3?
var dict: [AnyHashable: Any]
let b: AnyObject? = nil
let c = b as Any
dict = ["a": "aa", "b": c]
If I test
dict["b"] == nil
It returns false. Is it supposed to be right?
Can someone explain why this works in Swift 3?
var dict: [AnyHashable: Any]
let b: AnyObject? = nil
let c = b as Any
dict = ["a": "aa", "b": c]
If I test
dict["b"] == nil
It returns false. Is it supposed to be right?
On
You're running into nested optionals. If a dictionary holds a type E, then the dictionary access method returns a value of type E?, either the value if it exists, or nil.
In your case, you've created a dictionary where the value is an optional. So the E above is something like Any?. This means the return value of the getter is E? i.e., Any??
In your case, dict["b"] returns a non-nil optional, containing the value 'nil'
Putting your code in a playground and printing dict["b"] confirms this by printing the string Optional(nil)
Assuming you are using the latest Xcode, 8.1 (8B62) with Swift 3.0.1 .
Anycontainingnilis not so easy as simple nested optional:Nested Optional
b == nilreturnstrue.Anycontainingnilb == nilbecomesfalseas written by the OP.You can detect
nilinAny, with something like this:(This works in Swift 3.0.1, not in Swift 3.0.0 .)