ios - iOS/Objective-C 的 switch 语句中的 Fallthrough

标签 ios objective-c switch-statement

假设我有一个包含五个案例的 switch 语句,但只调用了两个真正的方法,如下所示:

switch (condition) {
    case conditionOutcome1: 

        [self firstMethod];
        break;

    case conditionOutcome2: 
        [self secondMethod];
        break;

    case conditionOutcome3:

        [self firstMethod];
        break;

    case conditionOutcome4: 

        [self firstMethod];

        break;
    case conditionOutcome5: 

        [self secondMethod];
        break;

    default:
        break;
}

像这样对案例进行分组是否安全?

    switch (condition) {

    case conditionOutcome1:
    case conditionOutcome3:
    case conditionOutcome4: 

        [self firstMethod];
        break;

    case conditionOutcome2: 
    case conditionOutcome5: 

        [self secondMethod];
        break;


    default:
        break;
}

它工作正常,但我以前从未在 objective-c 中使用过它,所以我想通过保存几行代码来确保我不会造成任何问题。

谢谢!

最佳答案

只是添加到其他答案中,它被称为fallthrough,您可以阅读它here .

关于ios - iOS/Objective-C 的 switch 语句中的 Fallthrough,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21136395/

相关文章:

c# - Xamarin Studio Storyboard​​空白

ios - 如何使用按钮在 Google ios sdk map 中设置放大和缩小

objective-c - 我们如何在 ios 中处理前台的耳机播放/暂停按钮事件

go - 如何从 golang 的开关盒内定义的函数内部跳出开关盒?

ios - 为 NSFetchedResultsController 创建关系谓词的正确方法

ios - Alamofire 3->4 Response 和 Response Serializer Swift 3.0 的问题

ios - UIButton 重叠并意外隐藏在 UITableViewCell 中

perl - 给定/何时存在什么样的简洁用例?

C# 7.0 通用参数上的案例模式匹配

ios - 使用自动布局将 UIButtons 设置为等距离