Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__VA_ARGS__ Macro expansion

I'm trying to get my macro to work like NSLog() which accepts variable arguments. Code below causes parse issues.

What is the correct way to define this?

#define TF_CHECKPOINT(f, ...) \
do { \
NSString *s = [[NSString alloc] initWithFormat:f arguments:__VA_ARGS__] autorelease]; \
[TestFlight passCheckpoint:[NSString stringWithFormat:@"%@: %@", [self class], s]]; \
} while (0)
like image 660
Morrowless Avatar asked Feb 19 '23 15:02

Morrowless


1 Answers

You forgot the opening bracket for the autorelease message.

Moreover -[NSString initWithFormat:arguments:] expects a va_list argument, whereas __VA_ARGS__ is replaced by all the passed arguments. Here, you need to use -[NSString initWithFormat:] or +[NSString stringWithFormat:].

Finally, you may prefix __VA_ARGS__ with ##. By doing so, the preceding comma is deleted when there is no argument.

Try this:

#define TF_CHECKPOINT(f, ...) \
do { \
NSString *s = [NSString stringWithFormat:(f), ##__VA_ARGS__]; \
[TestFlight passCheckpoint:[NSString stringWithFormat:@"%@: %@", [self class], s]]; \
} while (0)
like image 180
Nicolas Bachschmidt Avatar answered Mar 04 '23 04:03

Nicolas Bachschmidt