ios测试宏指令出错:“Expected identefier”

时间:2023-03-10 05:18:11
ios测试宏指令出错:“Expected identefier”

写了一个简单的测试宏指令,然后在下面代码中报错,不知道怎么修复?谢谢

#define test(condition) do{\
if (condition)
{\ //// <-----Expected identifier or (
NSlog @"passed: " %@ #condtion); \ }
else
{\
NSLog(@"failed: " @ #condition); \ }
} //// <-----extraneous closing brace ( "}")

2个回答

你应该在每行宏指令的末尾加一个反斜杠,最后一个不用加

#define test(condition) do{\
if (condition) \
{\
NSlog @"passed: " %@ #condtion); \
} \
else \
{ \
NSLog(@"failed: " @ #condition); \
} \
}

简单方法:

#define test(condition) NSLog("%s: %s\n", condition ? "Passed" : "Failed", #condition);

调用:

int i = 6;
test(i > 8);
test(i < 8);

输出:

Failed: i > 8
Passed: i < 8 转;http://ask.****.net/questions/1090