How to increment a value using a C-Preprocessor?
Posted
by mystify
on Stack Overflow
See other posts from Stack Overflow
or by mystify
Published on 2010-05-24T12:11:15Z
Indexed on
2010/05/24
14:31 UTC
Read the original article
Hit count: 190
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?
© Stack Overflow or respective owner