在C/C++/Objective C中,可以使用编译器预处理器定义宏。此外,您可以使用编译器预处理器包含/排除代码的某些部分。

#ifdef DEBUG
    // Debug-only code
#endif

Swift中有类似的解决方案吗?


当前回答

isDebug常量基于活动编译条件

另一个可能更简单的解决方案是,将DEBUG定义为项目构建目标的Active Compilation Conditions之一,并包含以下内容(我将其定义为全局常量):

#if DEBUG
    let isDebug = true
#else
    let isDebug = false
#endif

isDebug常量基于编译器优化设置

这个概念建立在kennytm的答案之上

与kennytm相比,其主要优势在于它不依赖于私有或未记录的方法。

在Swift 4中:

let isDebug: Bool = {
    var isDebug = false
    // function with a side effect and Bool return value that we can pass into assert()
    func set(debug: Bool) -> Bool {
        isDebug = debug
        return isDebug
    }
    // assert:
    // "Condition is only evaluated in playgrounds and -Onone builds."
    // so isDebug is never changed to true in Release builds
    assert(set(debug: true))
    return isDebug
}()

与预处理器宏和kennytm的答案相比,

✓ 您不需要定义自定义的-D DEBUG标志来使用它~它实际上是根据优化设置定义的,而不是Xcode构建配置✓ 已记录,这意味着该函数将遵循正常的API发布/弃用模式。✓ 在if/else中使用不会生成“将永远不会执行”警告。

其他回答

莫伊尼昂的回答很好。这是另一条信息,以防有帮助,

#if DEBUG
    let a = 2
#else
    let a = 3
#endif

您可以像下面这样否定宏,

#if !RELEASE
    let a = 2
#else
    let a = 3
#endif

我的两分钱用于Xcode 8:

a) 使用-D前缀的自定义标志工作正常,但。。。

b) 更简单的使用:

在Xcode 8中有一个新的部分:“活动编译条件”,已经有两行,用于调试和发布。

只需添加无D的定义。

![在Xcode 8及以上版本中,转到构建设置->搜索自定义标志]1

在代码中

 #if Live
    print("Live")
    #else
    print("debug")
    #endif

XCODE 9及以上

#if DEVELOP
    //print("Develop")
#elseif PRODUCTION
    //print("Production")
#else
    //
#endif

isDebug常量基于活动编译条件

另一个可能更简单的解决方案是,将DEBUG定义为项目构建目标的Active Compilation Conditions之一,并包含以下内容(我将其定义为全局常量):

#if DEBUG
    let isDebug = true
#else
    let isDebug = false
#endif

isDebug常量基于编译器优化设置

这个概念建立在kennytm的答案之上

与kennytm相比,其主要优势在于它不依赖于私有或未记录的方法。

在Swift 4中:

let isDebug: Bool = {
    var isDebug = false
    // function with a side effect and Bool return value that we can pass into assert()
    func set(debug: Bool) -> Bool {
        isDebug = debug
        return isDebug
    }
    // assert:
    // "Condition is only evaluated in playgrounds and -Onone builds."
    // so isDebug is never changed to true in Release builds
    assert(set(debug: true))
    return isDebug
}()

与预处理器宏和kennytm的答案相比,

✓ 您不需要定义自定义的-D DEBUG标志来使用它~它实际上是根据优化设置定义的,而不是Xcode构建配置✓ 已记录,这意味着该函数将遵循正常的API发布/弃用模式。✓ 在if/else中使用不会生成“将永远不会执行”警告。