How to check if `Any(not Any?)` is nil or not in swift
Using if case
:
You can use if case Optional<Any>.none = a
to test if a
is nil
:
var b: Bool?
var a = b as Any
if case Optional<Any>.none = a {
print("nil")
} else {
print("not nil")
}
nil
b = true
a = b as Any
if case Optional<Any>.none = a {
print("nil")
} else {
print("not nil")
}
not nil
Using switch
:
You can use the pattern test with switch
as well:
var b: Bool?
var a = b as Any
switch a {
case Optional<Any>.none:
print("nil")
default:
print("not nil")
}
nil