復(fù)制一段《The Swift Programming Language》上的代碼
let vegetable = "red pepper"
switch vegetable{
case "celery":
let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber","watercress":
let vegetableComment = "That would make a good tea sandwich."
//switch支持所有類型的數(shù)據(jù)赴穗,以及多種比較運(yùn)算——沒有限制為必須是整數(shù)谣沸,也沒有限制為必須測試相等(tests for equality 真的是這樣翻譯的嗎篓冲?)
case let x where x.hasSuffix("pepper"):
let vagetableComment = "Is it a spicy \(x)?"
//switch語句要求必須覆蓋所有的可能勒奇,否則報(bào)錯'switch must be exhaustive, consider adding a default cla'
default:
print("不能沒有default")
}
不需要寫break
執(zhí)行完匹配到的case后番川,程序會跳出switch闹炉,而不是繼續(xù)執(zhí)行下一個case诡必,所以不需要在case的代碼后面添加break來跳出switch奢方。