如何在swift中比较switch语句控制表达式与其模式?

时间:2015-04-21 14:23:11

标签: ios swift switch-statement compare expression

我试过这个并且不起作用:

    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语句中。

1 个答案:

答案 0 :(得分:2)

这是你需要的吗? (我不了解您对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
}