ios #ifdef DEBUG 与 #if DEBUG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16245633/
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 23:21:57  来源:igfitidea点击:

#ifdef DEBUG versus #if DEBUG

iosobjective-cxcode

提问by lidsinker

It is unclear to me when using compiler directives which of the below two code snippets is correct/preferred and why. It seems that most developers and Open Source projects I've seen use the first but I have seen the second used frequently as well.

在使用编译器指令时,我不清楚以下两个代码片段中的哪一个是正确/首选的以及为什么。似乎我见过的大多数开发人员和开源项目都使用第一个,但我也看到第二个经常使用。

#ifdef DEBUG
[self doSomethingOnlyWhenDebugging];
#endif

VERSUS

相对

#if DEBUG
[self doSomethingOnlyWhenDebugging];
#endif

Which of the above code snippets is preferable for running code only while debugging and why? My guess is that the first will run if DEBUG is defined as TRUE or FALSE where the second will run only if DEBUG is defined and set to TRUE. Is that correct?

上面哪个代码片段更适合仅在调试时运行代码,为什么?我的猜测是,如果 DEBUG 定义为 TRUE 或 FALSE,第一个将运行,而第二个将仅在 DEBUG 定义并设置为 TRUE 时运行。那是对的吗?

采纳答案by Jeff Kelley

You are correct. #if DEBUGwill not evaluate if DEBUGis defined as 0.

你是对的。#if DEBUG将不会评估 ifDEBUG被定义为0

As for when to use each, you can stick to using #ifdeffor anything where you only need to addcode if the preprocessor definition is present, such as adding debug logging. If you need to inspect the value and go down different compilation paths, then I would use a 0or 1. A good example of that is TARGET_IPHONE_SIMULATOR, which is always defined for an iOS project, but only 1if you're compiling for the simulator.

至于何时使用每个,如果存在预处理器定义,您可以坚持使用#ifdef仅需要添加代码的任何内容,例如添加调试日志记录。如果您需要检查值并沿着不同的编译路径,那么我会使用 a01。一个很好的例子是TARGET_IPHONE_SIMULATOR,它始终为 iOS 项目定义,但前提1是您为模拟器进行编译。

回答by DawnSong

As I know, the finest choice is:

据我所知,最好的选择是:

#ifndef DEBUG
    NSLog(@"-1");
#elif DEBUG == 0
    NSLog(@"0");
#else
    NSLog(@"%d", DEBUG);
#endif

then, you will know #ifndef DEBUGis preferred above all others.

然后,您就会知道#ifndef DEBUG是首选。

There is a simpler choice:

有一个更简单的选择:

#if DEBUG == 0 // DEBUG is not defined or defined to be 0
    // do sth
#else
    // do sth
#endif

However, if -Wundefcompiler flag is on, there might be a warningwith #if DEBUG == 0.

但是,如果-Wundef编译器标志是,有可能是一个警告,#if DEBUG == 0

回答by gnasher729

You need to look at the code where DEBUG gets defined or not defined, and write your code accordingly. With DEBUG, you will find that it is either not defined, or defined with a value of 1. So either #ifDEBUG or #ifdefDEBUG will work.

您需要查看定义或未定义 DEBUG 的代码,并相应地编写代码。使用 DEBUG,您会发现它要么未定义,要么定义为 1。因此#ifDEBUG 或#ifdefDEBUG 都可以工作。

For #define's that are under your control, I recommend that you always define them, either with a value of 0 or 1. You can then use #ifto check for the value, but you can also use them directly in an ordinary if statement or in an expression, which may make your code more readable. And since it is always defined, you can use "Jump To Definition" in Xcode to go to the place where it is defined and check how and why it is set. If instead you either #defineor not #definethe value, and it is not defined, then Xcode will have no idea where in your source code it is not defined. It also gives you a chance to look for misspelled uses. If "Jump to Definition" doesn't work, then you know you have spelled it wrong.

对于#define在您控制之下的 's,我建议您始终定义它们,使用值 0 或 1。然后您可以使用#if来检查该值,但您也可以直接在普通的 if 语句或 in 中使用它们一个表达式,它可以使您的代码更具可读性。并且由于它始终是定义的,因此您可以使用 Xcode 中的“跳转到定义”转到定义它的位置并检查它的设置方式和原因。如果您#define不是#define该值,并且它没有定义,那么 Xcode 将不知道它在您的源代码中的哪个位置没有定义。它还使您有机会查找拼写错误的用法。如果“跳转到定义”不起作用,那么您就知道自己拼错了。

BTW Within an #ifdirective, any macro that is not defined is replaced by 0. So #ifDEBUG will work if DEBUG is not defined, or if DEBUG is defined as 0, and it will not compile if DEBUG is defined as nothing - which will tell you what is wrong so you can fix it. From that point of view, using #ifis better unless it doesn't compile.

顺便说一句,在#if指令中,任何未定义的宏都被 0 替换。因此#if,如果未定义 DEBUG,或者 DEBUG 定义为 0,则 DEBUG 将起作用,如果 DEBUG 定义为空,则它不会编译 - 这会告诉你出了什么问题,所以你可以修复它。从这个角度来看,使用#if更好,除非它不编译。