如何在 ObjC 和 Swift 中设置 DEBUG 和 RELEASE 标志?

2,691 阅读1分钟

原文链接:kitefaster.com/2016/01/23/… ,版权归原作者所有

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 -DDEBUG to the Debug section
  • Add -DRELEASE to the Release section

DebugReleaseFlags

In Swift you can use the following code:

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

//or 

#if DEBUG //or RELEASE
// 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=1 to 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

Note: #ifdef instead of #if