1

Example: I want to do this:

METHODNAME(5) {
    // do something 
}

which results in:

- (void)animationStep5 {
    // do something 
}

Is there any way to do this? Basically, what I need is a way to generate a real source code string before the program is compiled, so the compiler does see - (void)animationStep5...

Or maybe there's something different than a macro, which can help here to auto-generate method names (not at run-time)?

1
  • Why? This seems like a gross abuse of the preprocessor. Commented May 24, 2010 at 14:54

3 Answers 3

2

As was already answered here, the objective-C preprocessor is very close to the C one.

You should have a look at the examples posted there, and have a look at C proprocessor. You will simply have to use the ## syntax of the preprocessor, to concatenate the method name, and the number you want.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the concatenation operator

#define METHODNAME(i) -(void)animationStep##i

you can call it like

METHODNAME(5){}

This expands to -(void)animationStep5{}

Comments

1

Assuming the objective-c preprocessor behaves the same as the standard C one, you can use something like:

#define PASTE(a, b) a##b
#define METHODNAME(n) PASTE(animationStep,n)

to join the required bits together. This means that

METHODNAME(5)

gets translated to

animationStep5

(you may need to add the "void" from your question to the macro definitino depending on exactly what it is you need to do).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.