Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether code is running in DEBUG / RELEASE build?

People also ask

What is the difference between build and debug?

Debug is the act of finding out where in the code the application is going wrong (debug = get rid of bugs.) In the context of an IDE, compile (Build in Visual Studio) just builds the code, debug (Run in Visual Studio) compiles the code, launches it and attaches a debugger.


Check your project's build settings under 'Apple LLVM - Preprocessing', 'Preprocessor Macros' for debug to ensure that DEBUG is being set - do this by selecting the project and clicking on the build settings tab. Search for DEBUG and look to see if indeed DEBUG is being set.

Pay attention though. You may see DEBUG changed to another variable name such as DEBUG_MODE.

Build Settings tab of my project settings

then conditionally code for DEBUG in your source files

#ifdef DEBUG

// Something to log your sensitive data here

#else

// 

#endif

For a solution in Swift please refer to this thread on SO.

Basically the solution in Swift would look like this:

#if DEBUG
    println("I'm running in DEBUG mode")
#else
    println("I'm running in a non-DEBUG mode")
#endif

Additionally you will need to set the DEBUG symbol in Swift Compiler - Custom Flags section for the Other Swift Flags key via a -D DEBUG entry. See the following screenshot for an example:

enter image description here


Apple already includes a DEBUG flag in debug builds, so you don't need to define your own.

You might also want to consider just redefining NSLog to a null operation when not in DEBUG mode, that way your code will be more portable and you can just use regular NSLog statements:

//put this in prefix.pch

#ifndef DEBUG
#undef NSLog
#define NSLog(args, ...)
#endif

Most answers said that how to set #ifdef DEBUG and none of them saying how to determinate debug/release build.

My opinion:

  1. Edit scheme -> run -> build configuration :choose debug / release . It can control the simulator and your test iPhone's code status.

  2. Edit scheme -> archive -> build configuration :choose debug / release . It can control the test package app and App Store app 's code status. enter image description here


Swift and Xcode 10+

#if DEBUG will pass in ANY development/ad-hoc build, device or simulator. It's only false for App Store and TestFlight builds.

Example:

#if DEBUG
   print("Not App Store build")
#else
   print("App Store build")
#endif