我试过了,但没用:

    let value1 = 12
    let sumOfOtherValues = 10 + 1 + 24

    switch value1 {
    case let (value1,sumOfOtherValues) where (value1 > sumOfOtherValues):
         break;
    case value1..>sumOfOtherValues:
         break;
    default:
         breaK;
    }

我真的想用switch语句而不是if语句来实现这一点。

最佳答案

这是你需要的吗?(我不明白你需要value1..>sumOfOtherValues

let value1 = 12
let sumOfOtherValues = 10 + 1 + 24

switch value1 {
case _ where sumOfOtherValues > value1:
    println("case 1")
    //break //it's not mandatory.
    fallthrough //Without this code, this will stop here if the switch match this case. If you want that your switch continue to search, add 'fallthrough' at the end of each case
case _ where value1 > sumOfOtherValues:
    println("case 2")
    break
default:
    break
}

关于ios - 如何快速比较switch语句控件表达式及其模式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29775007/

10-13 07:52