在Swift中使用“fallthrough”关键字有什么问题?

时间:2016-04-17 00:40:03

标签: swift

在下面的比较元组的switch语句中,我希望它能打印出“点在x轴上”和“在测试范围内”,而是在第一个匹配的情况下打印出所有内容。也许我误会了,但我认为在Swift中,通过允许你在输入案件时继续检查案件?

let switchPoint = (1,0)
switch switchPoint
{
    case (0,0):
        print("origin")
        fallthrough
    case (_,0):
        print("point is on x axis")
        fallthrough
    case (0,_):
        print("point is on y axis")
        fallthrough
    case (-2...2, -2...2):
        print("within test bounds")
        fallthrough
    default: print("outside test bounds")
}

1 个答案:

答案 0 :(得分:3)

来自apple的documentation on fallthrough:"一个fallthrough语句导致程序执行从switch语句中的一个case继续到下一个case。程序执行继续到下一个案例,即使案例标签的模式与switch语句的控制表达式的值不匹配。#34;

因此,Swift的落后与C行为一致。