1

In Swift, I can do:

#if DEBUG
    // code
#else
    // code
#endif

but making custom macros as described in #ifdef replacement in the Swift language answers doesn't work for me.

How am I supposed to do this for Swift 5 / Xcode 12?


Edit: My problem was that I assumed testing an application would trigger the macros in the application if I defined them in the AppTest target.

Putting -DYOURMACRO in Other Swift Flags under Swift Compiler - Custom Flags does indeed work.

The way to do what I wanted is either to put this flag in the application target whenever I want to run unit tests (really don't like this solution), or using launch arguments instead (not optimal but will do).

thanat0sis
  • 185
  • 1
  • 12

1 Answers1

1

In C, Objective-C and Metal you have to use #ifdef DEBUG (Xcode 12.5):

#import <Foundation/Foundation.h>

#ifdef DEBUG
    BOOL const DEBUG_BUILD = YES;
#else
    BOOL const DEBUG_BUILD = NO;
#endif

But in Swift you have to use just the following syntax (Xcode 12.5):

import Foundation

#if DEBUG
    let debugBuild: Bool = true
#else
    let debugBuild: Bool = false
#endif


And why you said you couldn't use a custom pre-processor macros?

#if !RELEASE
    import SceneKit
#endif

func loader() {
    #if DEBUG
        SceneKit.SCNSphere.init(radius: 0.1)
    #endif
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220