views:

38

answers:

1

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 use 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?

+1  A: 

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