Swift:Switch 语句失败行为 [英] Swift: Switch statement fallthrough behavior

查看:29
本文介绍了Swift:Switch 语句失败行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有这个:

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 语句的目标是打印每个 case 是否为真,而不是只打印第一个匹配的 case.我想我可以用 fallthrough 语句来做到这一点.然而,这让我质疑它是如何工作的.为什么即使 case 不匹配,fallthrough 也会自动打印下一个 case?我怎样才能让这个 switch 语句按照我想要的方式工作?

My goal with this switch statement is to have each case print if it is true and NOT to have only the first case that is matched print. I thought I could do this with the fallthrough statement. However, this has made me question how it works. Why does fallthrough automatically print the next case in line even if the case does not match? How could I go about making this switch statement work the way I want it to?

推荐答案

Fallthrough 进入下一个案例,而不是下一个匹配的案例.这个概念继承自 C switch 语句,其中每个 case 都可以被认为是一个 goto 目标标签,而 switch 语句将执行到第一个匹配的.

Fallthrough falls through to the next case, not to the next matching case. The concept is inherited from C switch statements, where each case may be thought of as a goto destination label, and the switch statement brings execution to the first matching one.

在 C 中,switch 语句仅指示在块内开始执行的位置.为了更加方便,您可以使用 break 语句跳过 switch 主体的其余部分,但没有什么强迫您这样做;如果你不这样做,执行会正常继续,就像 case 不存在一样.例如:

In C, the switch statement only dictates where execution starts inside the block. For added convenience, you may use the break statement to skip the rest of the switch body, but nothing forces you to; and if you don't, execution continues normally, like the cases weren't there. For instance:

switch (countdown)
{
    case 3: puts("3...");
    case 2: puts("2...");
    case 1: puts("1...");
    case 0: puts("0!");
}

在任何地方都没有 break,如果 countdown 是 3,那么你得到了整个事情(即使 countdown 显然是 3,而不是2、1 或 0).

With no break anywhere, if countdown is 3, then you get the whole thing (even though countdown is obviously 3, and not 2, 1 or 0).

当执行从一个 case 转到另一个 case 而不是退出 switch 作用域时(例如,使用 break 语句),您会失败"(即Swift fallthrough 关键字的作用).

When execution goes from a case to another instead of exiting the switch scope (for instance, with a break statement), you get "fall through" (which is what the Swift fallthrough keyword does).

这在 C 中是相关的,因为您可以在 switch 语句中使用任意复杂的结构,如果需要,可以重叠情况.这是一个合法的 C 程序:

This is relevant in C as you are allowed to use arbitrarily complex structures inside switch statements, overlapping cases if you want. Here is a legal C program:

switch (x)
{
    case 0:
        if (y == 3)
        {
    case 1:
            puts("hello");
        }
        else
        {
            puts("world");
        }

    case 2:
        puts("!");
}

然而,这种用法非常不常见,而且通常很难遵循(快!如果 x == 1 是否可以执行 else 分支?).我还没有测试过,但如果你能用 Swift 做类似的事情,我会感到非常惊讶.

This kind of use is however extremely uncommon and often hard to follow (quick! can the else branch be executed if x == 1?). I haven't tested, but I would be very surprised if you could do something similar with Swift.

一般来说,在 C 中,fall through 被认为是糟糕的风格,因为通常很难判断 fall through 是自愿的还是由于缺少 break 语句.Swift 使用 fallthrough 语句解决了这个问题,这使得你希望继续执行 switch 语句中的下一个 case 而不是退出 switch范围.

In general, in C, fall through is considered poor style since it is often hard to tell if the fall through is voluntary or due to a missing break statement. Swift solves this with the fallthrough statement, which makes it explicit that you want execution to continue to the next case inside the switch statement rather than exit the switch scope.

在你的情况下,你不能使用fallthrough来获得你想要的东西,因为fallthrough只有在你需要的执行序列是线性的时候才有用.您需要跳过无效的代码块,因此您需要使用 if-else 序列而不是 switch 语句.

In your case, you cannot use fallthrough to get what you want, because fallthrough is only useful when the execution sequence you need is linear. You need to skip over invalid blocks of code, so you need to use an if-else sequence instead of a switch statement.

这篇关于Swift:Switch 语句失败行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆