Reputation: 1841
I've created a .swift file that's mixed in with an Obj-C codebase and am wondering what the best way to set constants that are dependent on preprocessed flags for specific targets. Here's how I set constants for each target:
For "Target A", Under "Build Settings" -> "Swift Compiler - Custom Flags" -> Expand "Other Swift Flags" to reveal the following
Debug: -DDEBUG -DTARGET_A
Release: - DPRODUCTION -DTARGET_A
For "Target B", Under "Build Settings" -> "Swift Compiler - Custom Flags" -> Expand "Other Swift Flags"
Debug: -DDEBUG -DTARGET_B
Release: - DPRODUCTION -DTARGET_B
NotificationsHub.swift
#if TARGET_A
#if PRODUCTION
let HUBNAME = "hub1"
let HUBLISTENACCESS = "Endpoint=endpoint1"
#elseif DEBUG
let HUBNAME = "hub2"
let HUBLISTENACCESS = "Endpoint=endpoint2"
#endif
#elseif TARGET_B
#if PRODUCTION
let HUBNAME = "hub3"
let HUBLISTENACCESS = "Endpoint=endpoint3"
#elseif DEBUG
let HUBNAME = "hub4"
let HUBLISTENACCESS = "Endpoint=endpoint4"
#endif
#endif
class NotificationsHub: NSObject
{
...
let hubName = HUBNAME
let hubListenAccess = HUBLISTENACCESS
...
}
I don't like the ginormous nested if statement that precedes the class definition nor do I like setting constants outside of the class (are these global constants???) and would prefer to be able to specify each hubName and hubListenAccess in the project settings somewhere. In Obj-C, it's possible to specify values under "Build Settings" -> "Apple LLVM 7.0 Preprocessing" -> Expand "Preprocessor Macros", but don't know how I can do this in Swift.
Upvotes: 2
Views: 3877
Reputation: 201
With XCode 8 we could use the Active Compilation Conditions to add the MACRO without the -D prefix
Upvotes: 2