objective-c 如何声明仅调试语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/479531/
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
How do I declare a debug only statement
提问by Anthony Main
In C# I can use the following code to have code which only executes during debug build, how can I do the same in Xcode?
在 C# 中,我可以使用以下代码来获得仅在调试构建期间执行的代码,我如何在 Xcode 中执行相同的操作?
#if DEBUG
{
// etc etc
}
#endif
回答by Alnitak
You can use
您可以使用
#ifdef DEBUG
....
#endif
You'll need to add DEBUG=1to the project's preprocessor symbol definitions in the Debug configuration's settings as that's not done for you automatically by Xcode.
您需要DEBUG=1在调试配置的设置中添加项目的预处理器符号定义,因为 Xcode 不会自动为您完成。
I personally prefer doing DEBUG=1over checking for NDEBUG=0, since the latter implies that the default build configuration is with debug information which you then have to explicitly turn off, whereas 'DEBUG=1' implies turning ondebug only code.
我个人更喜欢对 进行DEBUG=1过度检查NDEBUG=0,因为后者意味着默认构建配置带有调试信息,然后您必须明确关闭该信息,而“DEBUG=1”意味着打开仅调试代码。
回答by ShuggyCoUk
The NDEBUG symbol should be defined for you already in release mode builds
应该已经在发布模式构建中为您定义了 NDEBUG 符号
#ifndef NDEBUG
/* Debug only code */
#endif
By using NDEBUG you just avoid having to specify a -D DEBUG argument to the compiler yourself for the debug builds
通过使用 NDEBUG,您只需避免为调试构建自己为编译器指定 -D DEBUG 参数
回答by Tibidabo
DEBUG is now defined in "debug mode" by default under Project/Preprocessor Macros. So testing it always works unless you have a very old project.
DEBUG 现在默认在“项目/预处理器宏”下定义为“调试模式”。所以测试它总是有效的,除非你有一个非常老的项目。
However I hate the fact that it messes up the code indentation and not particularly compact. That is why I use another macro which makes life easier.
但是我讨厌这样一个事实,即它弄乱了代码缩进并且不是特别紧凑。这就是为什么我使用另一个让生活更轻松的宏。
#ifdef DEBUG
#define DEBUGMODE YES
#else
#define DEBUGMODE NO
#endif
So testing the DEBUGMODE value is much more compact:
所以测试 DEBUGMODE 值要紧凑得多:
if (DEBUGMODE) {
//do this
} else {
//do that
}
My favourite:
我的最爱:
NSTimeInterval updateInterval = DEBUGMODE?60:3600;
回答by AnthonyLambert
There is a very useful debugging technote: Technical Note TN2124 Mac OS X Debugging Magic http://developer.apple.com/technotes/tn2004/tn2124.html#SECENVwhich contains lots of useful stuff for debugging your apps.
有一个非常有用的调试技术说明:Technical Note TN2124 Mac OS X Debugging Magic http://developer.apple.com/technotes/tn2004/tn2124.html#SECENV,其中包含许多用于调试应用程序的有用内容。
Tony
托尼

