#define指令问题

Ily*_*ski 0 c objective-c

我有以下代码:

#define checkLiteMessage    \
{   \
    #ifdef LITE_VERSION \
    if (alertView.tag == 100)   \
    {   \
        if (buttonIndex == 1)   \
        [ [UIApplication sharedApplication] openURL: buyAppLink];   \
        [alertView release];    \
        return; \
    }   \
    #endif  \
}   \
Run Code Online (Sandbox Code Playgroud)

我想要做的是每次调用checkLiteMessage时都要包含以下代码:

    #ifdef LITE_VERSION
    if (alertView.tag == 100)
    {
        if (buttonIndex == 1)
        [ [UIApplication sharedApplication] openURL: buyAppLink];
        [alertView release];
        return;
    }
    #endif
Run Code Online (Sandbox Code Playgroud)

我的问题是什么?为什么这段代码不能编译?

谢谢.

Chr*_*uin 8

您已使用行连续指定宏,这是正确的.但是,这意味着#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
Run Code Online (Sandbox Code Playgroud)

我要补充说,在宏中放置一个"返回"语句是非常邪恶的,并且会让每个人都感到困惑.不要这样做.