【问题标题】:How to fallthrough to a specific case in switch statement如何在 switch 语句中解决特定情况
【发布时间】:2016-05-05 17:11:38
【问题描述】:

在我的第一部分中,我根据行展示了不同的样式UIAlertController。第二部分做不相关的事情。为了避免cases 中的代码重复,我如何在 switch 语句中遇到特定情况?这可能很快吗?有没有其他语言有这个概念?

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    var alertController: UIAlertController!
    let cancelAction = UIAlertAction(title: L10n.Cancel.localized, style: .Cancel) { (action) in
        // ...
    }
    switch (indexPath.section, indexPath.row) {
    case (0, 0):
        alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
        //add other actions
    case (0, 1):
        alertController = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
        //add other actions
    case (0, _): //this case handles indexPath.section == 0 && indexPath.row != 0 or 1
        //I want this to be called too if indexPath.section is 0;
        //even if indexPath.row is 0 or 1.
        alertController.addAction(cancelAction)
        presentViewController(alertController, animated: true, completion: nil)
    default:
        break
    }
}

【问题讨论】:

  • 其实不可能。您只能fallthrough 连续病例。使用第二个开关或经典的if - else 表达式。

标签: swift switch-statement fall-through


【解决方案1】:

您目前尝试实现的目标似乎无法使用 Swift switch 语句实现。正如@AMomchilov 在另一个答案中提到的那样

默认情况下,Swift 中的 switch 语句不会从每个 case 的底部进入下一个 case。相反,整个 switch 语句在第一个匹配的 switch case 完成后立即完成执行,而不需要显式的 break 语句。

fallthrough 关键字似乎也不能解决问题,因为它不会评估案例条件:

fallthrough 语句会导致程序从 switch 语句中的一个 case 继续执行到下一个 case。即使 case 标签的模式与 switch 语句的控制表达式的值不匹配,程序也会继续执行下一个 case。

我认为最好的解决方案是拥有类似的东西

switch (indexPath.section, indexPath.row) {
case (0, _):
    if indexPath.row == 0 {
        alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
    }
    alertController = UIAlertController(title: nil, message: nil, preferredStyle: .Alert)
    alertController.addAction(cancelAction)
    presentViewController(alertController, animated: true, completion: nil)
default:
    break
}

【讨论】:

  • 还有其他语言有这种重新评估case条件的概念吗?
  • AFAIK,我认为任何其他语言都没有这个概念,尤其是编译的语言,因为在汇编中需要非常混乱的代码,这些代码会在每个块之后返回并重新评估条件跨度>
  • if 语句主体中的赋值在下一条指令中被破坏。
【解决方案2】:

您使用fallthrough 关键字。

没有隐式失败

与 C 和 Objective-C 中的 switch 语句相比,switch Swift 中的语句不会落在每个 case 的底部,并且 默认进入下一个。相反,整个 switch 语句 一旦第一个匹配的 switch case 完成它的执行 完成,不需要显式的 break 语句。这使得 switch 语句比 C 中的语句更安全、更易于使用 避免错误地执行多个 switch case。 -The Swift Programming Language (Swift 2.2) - Control Flow

但是,fallthrough 关键字只能用于添加功能。您不能让第一种情况和第二种情况相互排斥,也不能落入第三种情况。在您的情况下,将在 switch 语句之后无条件地重构常见情况,并将默认情况从 break 更改为 return

【讨论】:

  • 这行不通,问题是关于具体情况,即从第一次切换到第三次
  • 重新排序语句,然后
  • 是的,第一和第二似乎都需要降到第三
  • 那不是失败。 Fallthrough 只能添加功能。这是 GOTO 的工作,Swift 不支持。 presentViewController(alertController, animated: true, completion: nil) 行可以移动到 switch 语句之后,默认情况下是 returning 而不是 breaking
  • @osrl,这不是我在尝试更正答案时所说的吗?
猜你喜欢
  • 1970-01-01
  • 2010-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 2016-10-13
  • 1970-01-01
相关资源
最近更新 更多