How do I declare a debug only statement

前端 未结 4 1963
说谎
说谎 2021-01-31 02:44

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?

#if DEBUG
{
    // etc etc
}
#endif


        
相关标签:
4条回答
  • 2021-01-31 03:18

    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.

    Tony

    0 讨论(0)
  • 2021-01-31 03:24

    The NDEBUG symbol should be defined for you already in release mode builds

    #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

    0 讨论(0)
  • 2021-01-31 03:31

    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.

    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:

    if (DEBUGMODE) {
    //do this
    } else {
    //do that
    }
    

    My favourite:

    NSTimeInterval updateInterval = DEBUGMODE?60:3600; 
    
    0 讨论(0)
  • 2021-01-31 03:38

    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.

    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.

    0 讨论(0)
提交回复
热议问题