如何在Objective-C中的switch语句中使用goto? [英] How can I use goto in a switch statement in Objective-C?

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

问题描述

在我的代码中,我需要能够在同一个switch语句中跳转(转到)不同的情况。有没有办法做到这一点?

In my code I need to be able to jump (goto) a different case within the same switch statement. Is there a way to do this?

我的代码是这样的:(有很多代码我把它全部留下来了)

My code is something like this: (There is a lot of code I just left it all out)

switch (viewNumber) {
case 500:
        // [...]
break;

case 501:
        // [...]
break;
.
.
.
.
.

case 510:
        // [...]
break;

default:
break;

}

感谢您的时间!
-Jeff

Thank you for your time! -Jeff

推荐答案

通常非常糟糕的做法无条件地像你一样跳跃问。

It's generally very bad practice to unconditionally jump like you're asking.

我认为更可读/可维护的解决方案是将共享代码放在一个方法中,并且有多个案例调用该方法。

I think a more readable/maintainable solution would be to place the shared code in a method and have multiple cases call the method.

如果真的想要,你可以使用 goto 来做类似的事情:

If you really want to, you can use goto to do something like:

switch(viewNumber) {
    case 500:
        // [...]
        goto jumpLabel;
    case 501:
        // [...]
        break;
    case 502:
        // [...]
        jumpLabel:
        // Code that 500 also will execute
        break;
    default:break;
}

注意:我只提供上面的代码示例来回答你的问题。我现在感觉很脏,我可能要购买一些错误代码抵消

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

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