简体   繁体   English

如何在switch语句中陷入特定情况

[英]How to fallthrough to a specific case in switch statement

In my first section I show different style UIAlertController based on the row. 在我的第一部分中,我基于行显示了不同样式的UIAlertController Second section does unrelated stuff. 第二部分做无关的东西。 In order to avoid code duplication in both case s, how do I fallthrough to a specific case in switch statement? 为了避免两种case的代码重复,我如何在switch语句中陷入特定情况? Is this possible in swift? 这可能很快吗? Does any other language have this concept? 还有其他语言有这个概念吗?

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
    }
}

What you are trying to achieve currently doesn't seem to be possible with Swift switch statements. 当前使用Swift switch语句似乎无法实现的目标。 As mentioned in another answer by @AMomchilov 如@AMomchilov的另一个答案中所述

switch statements in Swift do not fall through the bottom of each case and into the next one by default. Swift中的switch语句默认不会落入每种情况的底部,而不会落入下一种情况。 Instead, the entire switch statement finishes its execution as soon as the first matching switch case is completed, without requiring an explicit break statement. 相反,整个switch语句将在第一个匹配的switch情况完成后立即完成其执行,而无需显式的break语句。

The fallthrough keyword also doesn't seem to solve the problem, since it won't evaluate the case conditions: fallthrough关键字似乎也无法解决问题,因为它不会评估案例条件:

A fallthrough statement causes program execution to continue from one case in a switch statement to the next case. fallthrough语句使程序执行从switch语句中的一种情况继续到另一种情况。 Program execution continues to the next case even if the patterns of the case label do not match the value of the switch statement's control expression. 即使case标签的模式与switch语句的控制表达式的值不匹配,程序也会继续执行下一个case。

I think that the best solution would be to have something like 我认为最好的解决方案是

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
}

You use the fallthrough keyword. 您使用fallthrough关键字。

No Implicit Fallthrough 没有隐式掉线

In contrast with switch statements in C and Objective-C, switch statements in Swift do not fall through the bottom of each case and into the next one by default. 与C和Objective-C中的switch语句相比,Swift中的switch语句不会掉入每种情况的底部,默认情况下不会掉入下一种情况。 Instead, the entire switch statement finishes its execution as soon as the first matching switch case is completed, without requiring an explicit break statement. 相反,整个switch语句将在第一个匹配的switch情况完成后立即完成其执行,而无需显式的break语句。 This makes the switch statement safer and easier to use than the one in C and avoids executing more than one switch case by mistake. 这使得switch语句比C语言中的语句更安全,更易于使用,并且避免了错误执行多个switch情况。 - The Swift Programming Language (Swift 2.2) - Control Flow -Swift编程语言(Swift 2.2)-控制流

However, the fallthrough keyword can only be used to add on functionality. 但是,fallthrough关键字只能用于添加功能。 You can't have the first and second case be mutually exclusive, and also fallthrough to the third case. 您不能让第一种情况和第二种情况互斥,也不能陷入第三种情况。 In your situation would refactor the common case to occur unconditionally after the switch statement, and change the default case from break to return . 在您的情况下,可以将普通情况重构为在switch语句后无条件发生,并将默认情况从break更改为return

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

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