Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expand a MACRO into NSString without using any string concatenation at runtime?

I defined a macro MYMACRO. Note: the value is not a valid NSString.

#define MYMACRO is

The macro is used inside the declaration of a NSString

@"This MYMACRO fun"

However, the preprocessor does not expand the macro. The preprocessed result is

@"This MYMACRO fun"

The best solution I found so far to get the macro to expand is:

#define MYMACRO @"is"
@"This "MYMACRO@" fun"

The macro expands as below which is a valid Objective-C syntax:

@"This "@"is"@" fun"

However, this requires 2 concatenation at runtime.

So my question is how to insert a macro insto a NSString without using any string concatenation at runtime ?

Ideally, I would like the runtime execute @"This is fun" rather than @"This "@"is"@" fun"

like image 407
David Andreoletti Avatar asked Jan 13 '12 01:01

David Andreoletti


1 Answers

I'm fairly certain what you have is already concatenating at compile time, and not runtime.

like image 119
Joshua Weinberg Avatar answered Sep 28 '22 22:09

Joshua Weinberg