Swift: how to use PREPROCESSOR Flags (like `#if DEBUG`) to implement API keys?
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
Conditional compiling in Swift
You can use like this :
class ViewController: UIViewController {
// Your common functions
}
#if UseAds
extension ViewController: XYZBannerDelegateProtocol {
// Your delegate methods
}
#endif
How to jump controller through debugging
yes you can use,
- place a breakpoint at
let vc = ViewController(nibName: "ViewController",bundle: nil)//here breakpoint set
- click the "Next" button for debug and in debug window paste this
line using po command and press enter.po navigationController?.pushViewController(vc, animated: true)
and now press the play button of debugging..it will execute your code.
How to determine whether code is running in DEBUG / RELEASE build?
Check your project's build settings under 'Apple LLVM - Preprocessing', 'Preprocessor Macros' for debug to ensure that DEBUG
is being set - do this by selecting the project and clicking on the build settings tab. Search for DEBUG
and look to see if indeed DEBUG
is being set.
Pay attention though. You may see DEBUG changed to another variable name such as DEBUG_MODE.
then conditionally code for DEBUG in your source files
#ifdef DEBUG
// Something to log your sensitive data here
#else
//
#endif
How to define a preprocessor symbol in Xcode
Go to your Target or Project settings, click the Gear icon at the bottom left, and select "Add User-Defined Setting". The new setting name should be GCC_PREPROCESSOR_DEFINITIONS
, and you can type your definitions in the right-hand field.
Per Steph's comments, the full syntax is:
constant_1=VALUE constant_2=VALUE
Note that you don't need the '='s if you just want to #define a symbol, rather than giving it a value (for #ifdef
statements)
Is there a preprocessor flag for Ad-Hoc builds?
The answer to this question goes into depth about how to create custom configurations and flags. Its not perfect but it works.
Still looking for an option that is more permanent and cross project compatible without having to spend time at the beginning of each project to set this up.
iOS, detect ad-hoc from code
Related Topics
Protocol Doesn't Conform to Itself
Index of a Substring in a String With Swift
Passing an Array to a Function With Variable Number of Args in Swift
Segue and Button Programmatically Swift
Swift Protocol Extension Method Is Called Instead of Method Implemented in Subclass
How to Compare One Value Against Multiple Values - Swift
Whither Dispatch_Once in Swift 3
Swift Lazy Instantiating Using Self
Get Terminal Output After a Command Swift
Main Thread Checker: Ui API Called on a Background Thread: -[Uiapplication Applicationstate]
How to Get Text to Wrap in a Uilabel (Via Uiviewrepresentable) Without Having a Fixed Width
Swiftui View and @Fetchrequest Predicate With Variable That Can Change
Weak References in Swift Playground Don't Work as Expected
How to Check If a Text Field Is Empty or Not in Swift
How to Set Up Scenekit Collision Detection
Swift 3 How to Display a Confirmation Screen Based on Mfmailcomposeresult Email Screen
Can You Execute an Applescript Script from a Swift Application