func getLatitude() -> Int {
return (-90...90).randomElement()!
}
let l = getLatitude()
let location = if l == 0 {
"Equator"
} else if l == 90 {
"north Pole"
} else if l == -90 {
"south Pole"
} else {
"not at the Equator or Pole"
}
print("latitude \(l): \(location)")
| Swift provides a shorthand spelling of if that you can use when setting values. In this if expression version, each branch contains a single value. If a branch’s condition is true, then that branch’s value is used as the value for the whole if expression in the assignment of weatherAdvice. Every if branch has a corresponding else if branch or else branch, ensuring that one of the branches always matches and that the if expression always produces a value, regardless of which conditions are true. |