我有以下代码:
#define checkLiteMessage \
{ \
#ifdef LITE_VERSION \
if (alertView.tag == 100) \
{ \
if (buttonIndex == 1) \
[ [UIApplication sharedApplication] openURL: buyAppLink]; \
[alertView release]; \
return; \
} \
#endif \
} \
我想做的是在每次调用 checkLiteMessage 时包含以下代码:
#ifdef LITE_VERSION
if (alertView.tag == 100)
{
if (buttonIndex == 1)
[ [UIApplication sharedApplication] openURL: buyAppLink];
[alertView release];
return;
}
#endif
我的问题是什么?为什么这段代码不能编译?
谢谢。
最佳答案
您指定了带有续行的宏,这是正确的。但是,这意味着 #ifdef 语句不在行的开头,因此预处理器不会执行它。
您不能将#ifdef 嵌入到宏中。你可以反过来:
#ifdef LITE_VERSION
#define checkLiteMessage do { \
if (alertView.tag == 100) \
{ \
if (buttonIndex == 1) \
[ [UIApplication sharedApplication] openURL: buyAppLink]; \
[alertView release]; \
return; \
} \
} while(0)
#else
#define checkLiteMessage do { ; } while (0)
#endif
我要补充一点,将“return”语句放在宏中是非常邪恶的,会让每个人都感到困惑。不要这样做。
关于c - #define 指令问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1144641/