适用于iOS / Objective-C的switch语句 [英] Fallthrough in switch statement for iOS/Objective-C

查看:122
本文介绍了适用于iOS / Objective-C的switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Say I have a switch statement with five cases, but only two real methods ever being called, like so:

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

将案件分组是否安全,如此?

Is it safe to group up the cases, like so?

    switch (condition) {

    case conditionOutcome1:
    case conditionOutcome3:
    case conditionOutcome4: 

        [self firstMethod];
        break;

    case conditionOutcome2: 
    case conditionOutcome5: 

        [self secondMethod];
        break;


    default:
        break;
}

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

It works fine, but I've never used it before in objective-c so I'd like to make sure I'm not causing any problems by saving a few lines of code.

谢谢!

推荐答案

是的,这样做很好。

我以前用过它。它会停止重复的代码。

I've used it before. It stops duplicate code.

这篇关于适用于iOS / Objective-C的switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆