Swift Functional Programming(Second Edition)
上QQ阅读APP看书,第一时间看更新

Switch

Swift provides the switch statement to compare a value against different matching patterns. The related statement will be executed once the pattern is matched. Unlike most other C-based programming languages, Swift does not need a break statement for each case and supports any value types. Switch statements can be used for range matching, and where clauses in switch statements can be used to check for additional conditions. The following example presents a simple switch statement with additional conditional checking:

let aNumber = "Four or Five" 
switch aNumber {
case "One":
let one = "One"
case "Two", "Three":
let twoOrThree = "Two or Three"
case let x where x.hasSuffix("Five"):
let fourOrFive = "it is \(x)"
default:
let anyOtherNumber = "Any other number"
}