How do I declare a debug only statement

前端 未结 4 1972
说谎
说谎 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条回答
  •  猫巷女王i
    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; 
    

提交回复
热议问题