ios - 如何在 swift 中将 switch 语句控制表达式与其模式进行比较?

标签 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 语句中进行此操作。

最佳答案

这是你需要的吗? (我不明白你需要 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 - 如何在 swift 中将 switch 语句控制表达式与其模式进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29775007/

相关文章:

ios - 组合重叠的 UIBezierPaths 以创建单个蒙版

ios - 什么是“iPhone音频工具”框中的音频电平表?

ios - 强制 SFSafariViewController 加载网站的桌面版本

swift - 如何使用 Swift 处理 Scenekit 中的碰撞检测?

ios - 将模型从 UINavigationView 转发到 UIView

swift - 在 Swift 中产生不同频率的声音

c - C语言中的有限状态机

switch-statement - 打开一个字节

ios - 所有 Storyboard、xib 文件都不会在 Macbook Pro M1 上使用 rosetta 更新框架或约束(遇到与 IBAgent-iOS 通信的错误)

switch-statement - 高效开关需要自认证吗?