开发者

How to make a big switch control structure with variable check values?

For example, I have a huge switch control structure with a few hundred checks. They're an animation sequence, which is numbered from 0 to n.

Someone said I can't us开发者_高级运维e variables with switch. What I need is something like:

NSInteger step = 0;
NSInteger i = 0;
switch (step) {
case i++:
    // do stuff
    break;

case i++:
    // do stuff
    break;

case i++:
    // do stuff
    break;

case i++:
    // do stuff
    break;

}

The point of this is, that the animation system calls a method with this big switch structure, giving it a step number. I want to be able to simply cut-copy-paste large blocks and put them in a different position inside the switch. for example, the first 50 blocks to the end.

I could do that easily with a huge if-else structure, but it would look ugly and something tells me switch is much faster.

How to?


By not doing it this way. A case label has to be a constant.

Here's one way to do this that might be better:

Define a selector for each thing you want to do e.g.

-(void) doOneThing;
-(void) doAnotherThing;
// etc

Put them in an array:

SEL anArray[] = { @selector(doOneThing), @selector(doAnotherThing) /* other selectors */, NULL };

And then you can just iterate through them.

SEL* selPtr = anArray;
while (selPtr != NULL)
{
    [self performSelector: *selPtr];
    selPtr++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜