The keyword let is used to define constants in Swift. But I keep finding let being used in if statements, and Ive been wondering why this is, or at least what the advantage to this is.
For example in this code:
if !session.setActive(false, error: &error) {
println("session.setActive fail")
if let e = error {
println(e.localizedDescription)
return
}
}
Why is error tested with a let in this statement: if let e = error ?
I understand why error needs testing, so we can make sure we can get at .localizedDesciption but I don't understand why we cant just do something like:
if error {
println(error.localizedDescription)
Outside of this example Ive also noticed let being used in a lot of other if statements. What are the advantages to this? I would love to know the thinking behind it.
Finally, can var be used in an if statement in the same way?