ARTICLES
How to specify DEBUG and RELEASE flags in Xcode with Swift or Objective-C
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
- Open your Project Build Settings and search for “Swift Compiler – Custom Flags” … “Other Swift Flags”.
- Add
-DDEBUGto the Debug section - Add
-DRELEASEto the Release section
- 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
- 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. - Add
RELEASE=1to the Release section - 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
Tested with the following versions:
- Xcode 7.2
- iOS 9.2.1
- tvOS 9.1
- Swift 2.1.1