1

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.

2 Answers 2

14

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)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.