很明顯swift的switch比oc的功能強化的多 下面是學(xué)習(xí)參考
<h5>不像 C 語言里的switch語句,在 Swift 中,switch語句不會一起匹配"a"和"A"。相反的,上面的代碼會引起編譯期錯誤:case "a": 不包含任何可執(zhí)行語句——這就避免了意外地從一個 case 分支貫穿到另外一個,使得代碼更安全、也更直觀。</h5>
先配個圖
區(qū)間匹配
let countedThings = "moons orbiting Saturn"
var naturalCount: String
switch approximateCount {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")
// 輸出 "There are dozens of moons orbiting Saturn."