在 OC 中我们设置编译条件,一般通过修改
Preprocessor Macros 中对应的宏定义。但是在Swift中,预处理的宏定义已经无效了,主要是 Swift 不在包含预处理的过程
The Swift compiler does not include a preprocessor. Instead, it takes advantage of compile-time attributes, build configurations, and language features to accomplish the same functionality. For this reason, preprocessor directives are not imported in Swift.
那么我们该如何在编译时做条件选择呢?
Swift中设置编译条件
在 Xcode 8 之前我们可以通过设置
Swift Compiler - Custom Flags 来设置我们需要用到的条件,在 Other Swift Flags 中设置 -D ADHOC
- Go to your project / select your target / Build Settings / search for Custom Flags
- For your chosen target set your custom flag using -D prefix (without white spaces), for both Debug and Release
- Do above steps for every target you have
在 Xcode 8 之后我们可以直接设置
Stackoverdflow 上边的回答