Mor*_*ess 3 macros objective-c variadic-functions
我试图让我的宏工作,就像NSLog()接受变量参数.下面的代码导致解析问题.
定义这个的正确方法是什么?
#define TF_CHECKPOINT(f, ...) \
do { \
NSString *s = [[NSString alloc] initWithFormat:f arguments:__VA_ARGS__] autorelease]; \
[TestFlight passCheckpoint:[NSString stringWithFormat:@"%@: %@", [self class], s]]; \
} while (0)
Run Code Online (Sandbox Code Playgroud)
您忘记了autorelease邮件的左括号.
而且-[NSString initWithFormat:arguments:]需要一个va_list说法,而__VA_ARGS__被所有传递的参数所取代.在这里,你需要使用-[NSString initWithFormat:]或+[NSString stringWithFormat:].
最后,你可以__VA_ARGS__加上前缀##.通过这样做,在没有参数时删除前面的逗号.
试试这个:
#define TF_CHECKPOINT(f, ...) \
do { \
NSString *s = [NSString stringWithFormat:(f), ##__VA_ARGS__]; \
[TestFlight passCheckpoint:[NSString stringWithFormat:@"%@: %@", [self class], s]]; \
} while (0)
Run Code Online (Sandbox Code Playgroud)