How can I use goto in a switch statement in Objective-C?

萝らか妹 提交于 2019-12-05 07:13:46

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.

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

Note: I only provided the code example above to answer your question. I now feel so dirty I might have to buy some Bad Code Offsets.

Instead of using goto, refactor your code so that the two (or more) cases that use common code instead call it in a common method.

Something like:

switch (value) {
   case (firstValue):
       // ...
       break;
   case (secondValue):
       [self doSharedCodeForSecondAndThirdValues];
       break;
   case (thirdValue):
       [self doSharedCodeForSecondAndThirdValues];
       break;
   default:
       break;
}

// ...

- (void) doSharedCodeForSecondAndThirdValues {
   // do stuff here that is common to second and third value cases
}

It wouldn't be the end of the world to use goto, though it is bad practice.

The practical reason for avoiding use of goto is that you have to search through your swtich-case tree to find that goto label.

If your switch logic changes, you'll have a messy situation on your hands.

If you pull out common code to its own method, the code is easier to read, debug and extend.

You should probably try rewrite your code, like a recursive call or just factor out common stuff and call a separate function. But as a fix and quick answer to your question you could put a label before your switch and goto it, like so

switchLabel:
switch(viewNumber) {
  case 500: {
    viewNumber = 501;
    goto switchLabel;
  }
}

Not sure of the Objective-C syntax here, but you could also try a variation thereof

int lastView = 0;

while (lastView != viewNumber)
  switch(lastView = viewNumber) {
    case 500: {
      viewNumber = 501;
      break;
    }
  }

which will keep on looping until the viewNumber doesn't change any more. This is still pretty much just a pretty-looking goto though.

And since we're doing gotos you could just goto into another case, as pointed out already. You could also do fancy stuff similar to Duff's device, by putting cases inside of other blocks. But that's just mad.. :)

[I'm making this answer community wiki because this doesn't actually answer the question per se]

As others have said, this is very bad style, and makes for unreadable code...

Alternatives:

  1. Factor the common code into a separate function, and call that in 2 places.
  2. Use fallthroughs, leave off the the break on a case and it falls through to the next one (remember, cases don't have to be in numerical order!)
  3. if you only want part of a case to be done in the other case, protect it with an if:

as in

case 500:
 .
 .
 .
case 501:
    if(viewNumber == 501)    {
     .
     .
     .
    }
     .
     .
     .
    break;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!