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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With