In Swift, why does
var x: Int? = nil
if let y: Int? = x { ... }
behave differently from
if let y: Int? = nil { ... }
if let
is used to get rid of the optional. If you want to see when the variable is nil use
if let y: Int = x {
// This occurs if you are able to give a non-nil value to the variable
} else {
// This occurs if the optional x is nil
}
as far as I know you shouldn't try declare the type of a constant in the if let
statement to be optional because that kind of defeats the purpose of the if let statement.