How to extend MACRO in NSString without using any string concatenation at runtime?

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

#define MYMACRO is 

Macro used inside an NSString declaration

 @"This MYMACRO fun" 

However, the preprocessor does not extend the macro. Pre-processed result

 @"This MYMACRO fun" 

The best solution I've found so far to deploy a macro:

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

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

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

However, execution requires 2 concatenations at runtime.

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

Ideally, I would like runtime to execute @"This is fun" , not @"This "@"is"@" fun"

+6
source share
2 answers

I'm pretty sure that you are already concatenating at compile time, not runtime.

+6
source
 #define MYMACRO @"is" [NSString stringWithFormat:@"This %@ fun",MYMACRO] 
-2
source

Source: https://habr.com/ru/post/905877/


All Articles