CarlJ CarlJ - 1 year ago 139
Objective-C Question

objC Preprocessor NSString Macro

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

(@"...") 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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download