views:

116

answers:

5

I'm trying to do something really trivial: A macro that takes an string and prints that to NSLog.

Like this:

#define PRINTTHIS(text) \
    NSLog(@"text");

However, when I try to pass a string to this guy I end up getting "text" printed to the console. Aren't all variables replaced on a string-level inside the macro? How to do that right?

+3  A: 

I think the problem you're running into is that "text" is inside what appears to the preprocessor as a string literal (the quotation marks, and possibly the @ symbol), so it's not replacing. Just a guess, but would this work?

#define PRINTTHIS(text) \
    NSLog(@"%@", text);

PRINTTHIS(@"string");

You could also define a version that takes C strings instead of ObjC strings:

#define PRINTTHIS_C(text) \
    NSLog(@"%s", text);

PRINTTHIS_C("string");
Tim
From the way he wrote it, it looks like he want to write PRINTTHIS(hello world) and have it do NSLog(@"Hello World").
Chuck
@Chuck: Yeah, but it'll freak out if he has a `%` in the string.
Wevah
A: 

Take this with a grain of salt...I'm not an Objective-C developer. But in C function-like macros, macro parameters appearing inside string literals are not replaced by their corresponding actual arguments, so the substitution you're trying to do won't work.

This might work:

#define PRINTTHIS(text) \
    NSLog(@text);

PRINTTHIS("your message here");

(so the quotes are moved out of the macro definition.)

Chuck points out in a comment that in Objective-C, the @ is considered part of the token in this construct. So it might be necessary to use the token pasting operator ## :

#define PRINTTHIS(text) \
        NSLog(@##text);
Jim Lewis
The Objective-C preprocessor works exactly the same way, except the @ is part of the token (it specifies an NSString literal, as opposed to "", which specifies a C-string literal). You're correct about the reason.
Chuck
Thanks Chuck! I've updated my answer; hopefully that will get walkthedog pointed in the right direction.
Jim Lewis
+1  A: 

Here's one way to write a macro that sticks its argument textually into a string object, though it strikes me as a bit gnarly:

#define PRINTTHIS(text) NSLog((NSString *)CFSTR(#text))

It uses the stringizing operator to turn the argument into a C string, which it then uses to create a CFString, which is toll-free bridged with NSString.

Chuck
+3  A: 

You'll want to use the preprocessor's 'stringizing' operator, #, and probably the 'token pasting' operator, '##':

#define STRINGIFY2( x) #x
#define STRINGIFY(x) STRINGIFY2(x)
#define PASTE2( a, b) a##b
#define PASTE( a, b) PASTE2( a, b)

#define PRINTTHIS(text) \
    NSLog(PASTE( @, STRINGIFY(text)));

I'm not sure if Objective-C requires the '@' to have no whitespace between it and the opening quote - if whitespace is permitted, drop the PASTE() operation.

Note that you'll need the goofy 2 levels of indirection for STRINGIFY() and PASTE() to work properly with macro parameters. And it pretty much never hurts unless you're doing something very unusual (where you don't want macro parameters expanded), so it's pretty standard to use these operators in that fashion.

Michael Burr
A: 

Use

#define PRINTTHIS(text) \
  do { NSLog(@"%s", #text); } while(0)

That way, text can contain % characters, its 'if-proof', the semi-colons are all in the right place, etc...

Personally, however, I'd say it makes more sense to just use NSLog() directly.

Jeff
That will print whatever expression you pass to the macro. If you pass `[foo objectAtIndex:idx]`, it will print “[foo objectAtIndex:idx]”. Assuming the questioner means to actually print the string, they should leave out the # operator, and may want to use `%@` instead of `%s` (depending on what kind of string it is).
Peter Hosey