CarlJ CarlJ - 4 months ago 26
Objective-C Question

objC Preprocessor NSString Macro

I have a problem to create a preprocessor macro function, that concatenates two Strings and "return" a

NSString
(@"...") value.

Here is what I tried:

#define ObjectKeyMake(NAME) @"com.test.##NAME"


if I print the result from a call I get:

NSLog(@"%@", ObjectKeyMake(foo)); // com.test.##NAME


so my question is: How can i concatenate 2 Strings in a preprocessor macro and "return" a NSString (@"..") ?

and no I can't use
#define ObjectKeyMake(NAME) [@"com.test." stringByAppendingString: NAME]
because i need a compile-time constant.

Answer

You can take advantage of the fact that the compiler combines string literals that are next to each other, like this:

NSString* greeting = @"Hello, " "world";

The macro implementation would look like this:

#define ObjectKeyMake(NAME) (@"com.test." #NAME)