English 中文(简体)
How can I use goto in a switch statement in Objective-C?
原标题:

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;

}

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.

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;




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签