In my code i have a lot of code like:
if (block) block(....)
So I want to define a macro, something like
#define safetyCall(block, ...) if((block)) {block(##__VA_ARGS__)};
But i couldn't get it to work. Any idea?
In my code i have a lot of code like:
if (block) block(....)
So I want to define a macro, something like
#define safetyCall(block, ...) if((block)) {block(##__VA_ARGS__)};
But i couldn't get it to work. Any idea?
你不需要##
和;
需要移动:
#define safetyCall(block, ...) if((block)) { block(__VA_ARGS__); }
如果您的块是内联的并且包含具有一系列逗号分隔字符串等的代码,这可能会遇到问题。
例子:
safetyCall(^void() {
NSArray *foo = @[@"alice", "bob"];
};
编译器会抱怨“预期的']'或'.'”和“预期的标识符或'('”。
但是,如果您将内联块声明为宏之前的单独块,则不会产生错误。
例子:
void (^fooBlock)(void) = ^void() {
NSArray *foo = @[@"alice", @"bob"];
}
safetyCall(fooBlock);