如何声明仅调试语句

时间:2021-03-09 20:57:20

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
}

4 个解决方案

#1


38  

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参数

#2


62  

You can use

您可以使用

#ifdef DEBUG
    ....
#endif

You'll need to add DEBUG=1 to 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=1 over 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 on debug only code.

我个人更喜欢在检查NDEBUG = 0时执行DEBUG = 1,因为后者暗示默认构建配置带有调试信息,然后必须明确关闭,而'DEBUG = 1'意味着启用仅调试代码。

#3


13  

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; 

#4


5  

There is a very useful debugging technote: Technical Note TN2124 Mac OS X Debugging Magic http://developer.apple.com/technotes/tn2004/tn2124.html#SECENV which contains lots of useful stuff for debugging your apps.

有一个非常有用的调试技术说明:技术说明TN2124 Mac OS X调试魔术http://developer.apple.com/technotes/tn2004/tn2124.html#SECENV,其中包含许多用于调试应用程序的有用内容。

Tony

托尼

#1


38  

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参数

#2


62  

You can use

您可以使用

#ifdef DEBUG
    ....
#endif

You'll need to add DEBUG=1 to 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=1 over 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 on debug only code.

我个人更喜欢在检查NDEBUG = 0时执行DEBUG = 1,因为后者暗示默认构建配置带有调试信息,然后必须明确关闭,而'DEBUG = 1'意味着启用仅调试代码。

#3


13  

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; 

#4


5  

There is a very useful debugging technote: Technical Note TN2124 Mac OS X Debugging Magic http://developer.apple.com/technotes/tn2004/tn2124.html#SECENV which contains lots of useful stuff for debugging your apps.

有一个非常有用的调试技术说明:技术说明TN2124 Mac OS X调试魔术http://developer.apple.com/technotes/tn2004/tn2124.html#SECENV,其中包含许多用于调试应用程序的有用内容。

Tony

托尼