【发布时间】:2015-09-02 02:20:21
【问题描述】:
目前我有这个:
let somePoint = (1, 0)
switch somePoint {
case (0,0):
print("origin") // does not print
fallthrough
case (_, 0):
print("y-axis") // prints y-axis. this makes sense
fallthrough
case(0, _):
print("x-axis") // prints x-axis (because of fallthrough? this should not print)
fallthrough
case(-2...2, -2...2):
print("in 5x5 box about the origin") // this prints and makes sense
default:
print("somewhere else") // does not print
}
我使用此 switch 语句的目标是打印每个案例(如果它是真的),而不是只打印第一个匹配的案例。我想我可以用 fallthrough 声明来做到这一点。然而,这让我质疑它是如何工作的。为什么即使大小写不匹配,fallthrough 也会自动打印行中的下一个案例?我怎样才能让这个 switch 语句按我想要的方式工作?
【问题讨论】: