在 Swift 中,一旦达到切换条件,它就会隐式“断开”并退出切换案例。换句话说,它不会继续下一个条件。如何在 C , C ++ , java , javascript 等中实现常规行为??
答案 0 :(得分:12)
如果您确实需要C风格的穿透行为,则可以使用 fallthrough 关键字逐个选择加入此行为。以下示例使用fallthrough创建数字的文本描述:
let integerToDescribe = 5
var description = "The number \(integerToDescribe) is"
switch integerToDescribe {
case 2, 3, 5, 7, 11, 13, 17, 19:
description += " a prime number, and also"
fallthrough // explicitly tells to continue to the default case
default:
description += " an integer."
}
println(description)
// prints "The number 5 is a prime number, and also an integer."