2

Possible Duplicate:
Can you make an incrementing compiler constant?

Example: I try to do this:

static NSInteger stepNum = 1;
#define METHODNAME(i) -(void)step##i
#define STEP METHODNAME(stepNum++)

@implementation Test

STEP {
    // do stuff...

    [self nextFrame:@selector(step2) afterDelay:1]; 
}

STEP {
    // do stuff...

    [self nextFrame:@selector(step3) afterDelay:1]; 
}

STEP {
    // do stuff...

    [self nextFrame:@selector(step4) afterDelay:1]; 
}

// ...

When building, Xcode complains that it can't increment stepNum. This seems logical to me, because at this time the code is not "alive" and this pre-processing substitution stuff happens before actually compiling the source code. Is there another way I could have an variable be incremented on every usage of STEP macro, the easy way?

Community
  • 1
  • 1
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260

2 Answers2

1

It seems to me that the fundamental problem is having these numbered variables, which are really just a poor man's array. An array would be the idiomatic way to do this in Objective-C.

Chuck
  • 234,037
  • 30
  • 302
  • 389
1

Not a chance that will work. For a start, stepNum isn't a preprocessor macro so the preprocessor just thinks of it as a load of characters. Explicitly naming the steps would definitely be a good thing. Your macro doesn't save much typing and obfuscates the code, even if you could get it to work.

Anyway, this is the wrong way to do what you want. You actually seem to be reinventing program control flow.

ETA: It occurs to me that my answer to your other question might help here. You manually number all of your methods, but then put all their selectors in an array. You then iterate through the array and the order is determined by the order you put them in the array.

Community
  • 1
  • 1
JeremyP
  • 84,577
  • 15
  • 123
  • 161