This tutorial shows you how to selectively run your debug or production code using flags.  Sometimes you may find it necessary to only run some code in Debug mode and other code in Release mode.

Swift

  1. Open your Project Build Settings and search for “Swift Compiler – Custom Flags” … “Other Swift Flags”.
  2. Add “-DDEBUG” to the Debug section
  3. Add “-DRELEASE” to the Release section
    • DebugReleaseFlags
  4. In Swift you can use the following code:
    #if RELEASE
        // release only code
    #else
        // debug only code
    #endif
    

    or

    #if DEBUG
        // debug only code
    #else
        // release only code
    #endif

Objective-C

  1. In Objective-C, the DEBUG flag is already defined as a preprocessor macro.  However, you will need to add the RELEASE flag by going to your Project Build Settings and searching for “Preprocessor Macros”.
  2. Add “RELEASE=1” to the Release section
  3. In Objective-C you can ue the following code:
    #ifdef RELEASE
        // release only code
    #else
        // debug only code
    #endif
    

    or

    #ifdef DEBUG
        // debug only code
    #else
        // release only code
    #endif

 

 

Get insights into your Apple Search Ads performance and optimize your bids and keywords for maximum revenue with Kitemetrics by Kitefaster.

 

Tested with the following versions:

  • Xcode 7.2
  • iOS 9.2.1
  • tvOS 9.1
  • Swift 2.1.1
How to specify DEBUG and RELEASE flags in Xcode with Swift or Objective-C
Translate »