ios #if 和 #ifdef Objective-C 预处理器宏之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12782289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:39:10  来源:igfitidea点击:

What's the difference between #if and #ifdef Objective-C preprocessor macro?

iosobjective-cmacrosc-preprocessorconditional-compilation

提问by Geri Borbás

How to define preprocessor macros in build settings, like IPAD_BUILD, and IPHONE_BUILD (and how to use them in my factory methods)?

如何在构建设置中定义预处理器宏,例如 IPAD_BUILD 和 IPHONE_BUILD(以及如何在我的工厂方法中使用它们)?

I'm using these by heart now, would be cool to know what is going behind.

我现在正在用心使用这些,知道后面发生了什么会很酷。

回答by Stas

/#if works as usual if:

/#if 正常工作,如果:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
  if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    return YES;
  }
#endif
  return NO;
}

/#ifdef means "if defined - some value or macros":

/#ifdef 表示“如果已定义 - 某些值或宏”:

#ifdef    RKL_APPEND_TO_ICU_FUNCTIONS
#define RKL_ICU_FUNCTION_APPEND(x) _RKL_CONCAT(x, RKL_APPEND_TO_ICU_FUNCTIONS)
#else  // RKL_APPEND_TO_ICU_FUNCTIONS
#define RKL_ICU_FUNCTION_APPEND(x) x
#endif // RKL_APPEND_TO_ICU_FUNCTIONS

or:

或者:

#ifdef __OBJC__
    #import <Foundation/Foundation.h>
#endif

Use this link for more information http://www.techotopia.com/index.php/Using_Objective-C_Preprocessor_Directives

使用此链接获取更多信息 http://www.techotopia.com/index.php/Using_Objective-C_Preprocessor_Directives

To test whether you running iPad or not you should have smth like this:

要测试您是否运行 iPad,您应该像这样:

#define USING_IPAD UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad

if (USING_IPAD) {
    NSLog(@"running iPad");
}

Here's another useful preprocessor functions:

这是另一个有用的预处理器函数:

#ifdef DEBUG
    //here we run application through xcode (either simulator or device). You usually place some test code here (e.g. hardcoded login-passwords)
#else
    //this is a real application downloaded from appStore
#endif

回答by gnasher729

A macro can be undefined, it can be defined with no value, or it can be defined with some value, possibly a number. Examples:

宏可以是未定义的,也可以不带值定义,或者可以用某个值(可能是数字)来定义。例子:

#undef MACRO
#define MACRO
#define MACRO ??????
#define MACRO 0
#define MACRO 1

#ifdef MACRO or #if defined (MACRO) checks whether the macro is defined, with or without value.

#ifdef MACRO 或 #if defined (MACRO) 检查宏是否已定义,有或没有值。

#if MACRO substitutes the macro definition; if the macro is not defined then it substitutes 0. It then evaluates the expression that it find. If we take the five examples above, #if MACRO will be turned into

#if MACRO 替换宏定义;如果未定义宏,则将其替换为 0。然后计算它找到的表达式。如果我们拿上面的五个例子,#if MACRO 就会变成

#if 0
#if
#if ??????
#if 0
#if 1

Number 2 and 3 give a compile time error. Number 1 and 4 evaluate to false, so the following code is skipped. Number 5 evaluates to true.

数字 2 和 3 给出了编译时错误。数字 1 和 4 的计算结果为 false,因此跳过以下代码。数字 5 评估为真。

#if is more flexible: You could write

#if 更灵活:你可以写

#if MACRO == 2

which will only compile the following code if the macro was defined for example as

如果宏被定义为例如,它只会编译以下代码

#define MACRO 2