Apple included full support for Swift preprocessor flags as of Xcode 8, so it's no longer necessary to set these values in "Other Swift Flags".
The new setting is called "Active Compilation Conditions", which provides top-level support for the Swift equivalent of preprocessor flags. You use it in exactly the same way as you would "Other Swift Flags", except there's no need to prepend the value with a "-D" (so it's just a little cleaner).
From the Xcode 8 release notes:
Active Compilation Conditions
is a new build setting for passing conditional compilation flags to the Swift compiler. Each element of the value of this setting passes to swiftc prefixed with-D
, in the same way that elements ofPreprocessor Macros
pass to clang with the same prefix. (22457329)
You use the above setting like so:
#if DEBUG
let accessToken = "DebugAccessToken"
#else
let accessToken = "ProductionAccessToken"
#endif
UPDATED: Xcode 8 now supports this automatically, see @DanLoewenherz's response above.
Prior to Xcode 8, you could still use Macros in the same way:
#if DEBUG
let apiKey = "KEY_A"
#else
let apiKey = "KEY_B"
#endif
However in order for them to be picked up by Swift, you need to set "Other Swift Flags" in your target's Build Settings:
-D
flagAs a follow up observation, try not to keep api keys / secrets in plaintext in the repository. Use a secrets management system to load the keys / secrets into the user's environment variables. Otherwise step 1 is necessary, if acceptable.
../set_keys.sh
that contains a list of export API_KEY_A='<plaintext_key_aef94c5l6>'
(use single quote to prevent evaluation)source ../set_keys.sh
and move it to the top of execution orderAPI_KEY_A="$API_KEY_A"
That captures the environment variable into the compiler define which is later used in each clang invocation for each source file.
Example directory structure
[10:33:15] ~/code/memo yes? tree -L 2 .
.
├── Memo
│ ├── Memo
│ ├── Memo.xcodeproj
│ ├── Memo.xcworkspace
│ ├── Podfile
│ ├── Podfile.lock
│ └── Pods
└── keys
In swift packages you have to do this inside of the swiftSettings
argument to .target
in your Package.swift
file. Use the define
method (Apple documentation) or Swift documentation
targets: [
.target(name: String,
dependencies: [Target.Dependency],
path: String?,
exclude: [String]?,
sources: [String]?,,
cSettings: [CSetting]?,
cxxSettings: [CXXSetting]?,
swiftSettings: [SwiftSetting]?,
linkerSettings: [LinkerSetting]?),
Mine looks like this and it works!
swiftSettings: [
.define("VAPOR")
]
in my code I can conditionally compile using this:
#if VAPOR
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With