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

In my code, I need to be able to switch (switch) to another case within the framework of one switch statement. Is there any way to do this?

My code looks something like this: (There is a lot of code, I just left everything)

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

}

Thank you for your time! -Jeff

+6
objective-c iphone switch-statement goto
source share
4 answers

As a rule, a very bad practice jumps unconditionally, as you ask.

I think a more readable / supported solution would be to put common code in a method and call a few cases of the method.

If you really want this, 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 just provided the above code example to answer your question. Now I feel so dirty that I may have to buy Bad Offsets .

+17
source share

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

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 would not be the end of the world to use goto , although this is bad practice.

The practical reason to avoid using goto is that you need to search the swtich-case tree to find the goto label.

If your switching logic changes, you will have an unpleasant situation at your fingertips.

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

+4
source share

You should probably try rewriting your code, such as a recursive call, or just drop common things and call a separate function. But as a correction and quick answer to your question, you could put a shortcut in front of your switch and go to it, for example,

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

Not sure about Objective-C syntax here, but you can also try its variant

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

which will continue the loop until viewNumber no longer changes. However, it is still a pretty pretty sight.

And since we make gotos, you can just go into another matter, as already indicated. You could also pretend to be like a Duff device by placing cases inside other blocks. But this is just crazy .. :)

0
source share

[I am making this answer to the community wiki because it does not actually answer the question on its own]

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

Alternative:

  • Designate the common code in a separate function and call it in two places.
  • Use the dips, leave the ditch on the case, and it will go on to the next one (remember that cases should not be in the order of numbers!)
  • If you want part of the job to be done otherwise, protect it with if:

how in

 case 500: . . . case 501: if(viewNumber == 501) { . . . } . . . break; 
-one
source share

All Articles