简体   繁体   English

如何使用where关键字快速检查switch语句中的所有情况?

[英]How to check all cases in a switch statement in swift using where keyword?

when I execute this code just the print("it is greater than zero") gets executed but I have two cases where it's true, I've tried to use the fallthrough keyword but it executes the next case block even if it's false, no matter what, 当我执行此代码时,只执行了print("it is greater than zero") ,但是有两种情况为真,我尝试使用fallthrough关键字,但是即使它为false,它也会执行下一个case块,否有什么关系,

which in turn raises another question, when should I use fallthrough keyword? 这又引发了另一个问题,何时应使用fallthrough关键字? if I want to forcefully execute the next block why don't just insert the code into the same block where fallthrough sits? 如果我要强制执行下一个块,为什么不只是将代码插入到在同一个块fallthrough坐镇?

Is there any way that the example below could print all cases that evaluate to true and still rule out all cases that evaluate to false? 有什么办法可以使下面的示例打印出所有评估结果为true的案例,但仍排除所有评估结果为false的案例?

let number = 1

switch number {
case _ where number > 0:
    print("it is greater than zero")
case _ where number < 2:
    print("it is less than two")
case _ where number < 0:
    print("it is less than zero")
default:
    print("default")
}

Thank you in advance for your answers! 预先感谢您的回答!

The switch statement isn't for this purpose, and doesn't work this way. switch语句不是用于此目的,并且不能以这种方式工作。 It's intent to to find a single true case. 目的是找到一个真实的案例。 If you want to check multiple cases, that's just an if statement: 如果要检查多种情况,那只是一个if语句:

let number = 1

if number > 0 {
    print("it is greater than zero")
}
if number < 2 {
    print("it is less than two")
}
if number < 0 {
    print("it is less than zero")
}

There is no equivalent switch for this. 没有与此等效的switch They're different control statements. 它们是不同的控制语句。

As you've discovered, fallthrough exists to allow two cases to run the same block. 正如您所发现的,存在fallthrough以允许两种情况运行同一块。 That's what it's for; 这就是它的目的; it doesn't check the other cases. 它不会检查其他情况。 As a rule, if you're using case _ extensively, you're probably not using switch correctly in Swift and should be using if . 通常,如果您广泛使用case _ ,则可能未在Swift中正确使用switch ,应使用if

You are correct that fallthrough means "do the next case without checking its truth value". 您是正确的,即fallthrough意味着“在检查其真值的情况进行下一种情况 ”。

So if you want to execute the first and second cases just in the situation where both are true, you must perform the second check as part of the first case. 因此,如果只想在两种情况都成立的情况下执行第一种情况和第二种情况,则必须将第二种检查作为第一种情况的一部分进行。 The minimal change from your code would thus be: 因此,对您的代码的最小更改是:

let number = 1
switch number {
case _ where number > 0:
    print("it is greater than zero")
    if number < 2 { fallthrough } // <--
case _ where number < 2:
    print("it is less than two")
case _ where number < 0:
    print("it is less than zero")
default:
    print("default")
}

However, that's not how I would write this particular example. 但是,这不是我编写此特定示例的方式。 And in any case you still face the problem of what should happen when the number is, say, -1; 而且无论如何,您仍然面临当数字为-1时会发生什么的问题。 that is less than 2 but also less than 0, so you face the same issue again. 小于2但小于0,因此您再次遇到相同的问题。 It is not at all obvious from your question what the actual goal is here! 从您的问题根本看不出这里的实际目标是什么! If those are really the three things you want to detect, you'd be better off using two separate tests, as they are not cleanly related to one another. 如果这确实是您要检测的三件事,那么最好使用两个单独的测试,因为它们之间并不是很明确的关联。 For example: 例如:

let number = 1
switch number {
case ..<0:
    print("it is less than zero")
case 0...:
    print("it is zero or greater")
default: break
}
switch number {
case ..<2:
    print("it is less than two")
default: break
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM