在swift 中使用switch 开关语句在进行匹配的时候不需要在匹配成功的地方加上break了,在swift中它会自动在匹配成功的地方跳出去。不会在向下面执行
example:
“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."
case let x where x.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy \(x)?" //在此处跳出开关语句不在往下面执行
default:
let vegetableComment = "Everything tastes good in soup."
}”
Swift 中的开关语句switch在swift中的使用,布布扣,bubuko.com
时间: 2024-12-23 07:14:07