Running the below piece of code on Xcode 9 playground, I noticed that nil does not equal nil. How can we check if b is in fact nil at line #4?
let s: String? = nil
var a: Optional<Any> = .some(s as Any)
if let b = a {
if b == nil {
print("b equals nil")
} else {
print("b doesn't equal nil. b is \(b)")
}
}
UPDATE 1:
I do understand why the behavior is so. What I am looking for is how to check if b is nil since comparing it with nil doesn't work here.
UPDATE 2:
To avoid confusion, I changed the var name to b
at line 3 (if let b = a)
UPDATE 3:
The answer turns out to be like this:
let s: String? = nil
var a: Optional<Any> = .some(s as Any)
if let b = a {
if Mirror(reflecting: b).descendant("Some") as? String == nil {
print("b equals nil")
} else {
print("b doesn't equal nil. b is \(b)")
}
}