In Swift, casting an optional constant to Any
is legal, for example:
let nilValue: String? = nil
let any: Any = nilValue
without the need to declare any
as optional Any (Any?
)
however, the surprising result for me is that:
print(any == nil) // false
print(any) // nil
Which seems to be conflict.
I could find a workaround to check whether any
is nil
or not, however my question is what is the reason for such a behavior?
Please note that if I declared any
as:
let any: Any? = nilValue
the output of
print(any == nil)
would be true
.