CarlJ
CarlJ

Reputation: 9481

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.

Upvotes: 1

Views: 3379

Answers (2)

dawenhing
dawenhing

Reputation: 7

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

Upvotes: -1

Tom Dalling
Tom Dalling

Reputation: 24125

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)

Upvotes: 14

Related Questions