c - #define 指令问题

标签 c objective-c

我有以下代码:

#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/

相关文章:

c - Bison 意外标识符错误

c - 如何计算 ANSI C 中整数数据类型的范围

iOS - 没有滑动背景图像的pushViewController

objective-c - iOS UISearchBar 文本字段背景

iphone - 如何在iphone中获取通话记录?

c - 如何正确实现 float 乘法(软件FP)

c - 在Linux终端中使用gcc编译C文件时出错

c - Gtk 与 Eclipse CDT

iphone - 如何将 nsnumber 转换为 float

iPhone 崩溃日志 - 是什么原因?