SWIFT: How to use PreProcessor Flags to implement API keys?

In Objective-C, static string constants are sometimes used to define alternate API keys (for example, the RELEASE and DEBUG keys to distinguish analysis packages, such as MixPanel, Flurry or Crashlytics): < p>

#if DEBUG
static NSString *const API_KEY = @"KEY_A";
#else
static NSString *const API_KEY = @"KEY_B";
#endif

Then…

[Analytics startSession:API_KEY];

This is How to convert to Swift, because the Swift compiler no longer uses the preprocessor?

As of Xcode 8, Apple fully supports the Swift preprocessor flag, so it is no longer necessary to Set these values ​​in Swift Flags.

The new setting is called “Active Compilation Conditions”, which provides top-level support for Swift equivalent preprocessor flags. The way you use it is similar to “Other Swift flag” is exactly the same, except that there is no need to use the “-D” prefix (so it is just a bit 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 -< /code>D, in the same way that elements of Preprocessor Macros pass to clang with the same prefix. (22457329)

enter image description here

You use the above settings as follows:

< pre>#if DEBUG
let accessToken = "DebugAccessToken"
#else
let accessToken = "ProductionAccessToken"
#endif

< p>In Objective-C, static string constants are sometimes used to define alternate API keys (e.g., RELEASE and DEBUG key, such as MixPanel, Flurry or Crashlytics):

#if DEBUG
static NSString *const API_KEY = @"KEY_A";
#else
static NSString *const API_KEY = @"KEY_B";
#endif

Then...

[Analytics startSession: API_KEY];

How is this converted to Swift, because the Swift compiler no longer uses the preprocessor?

As of Xcode 8, Apple fully supports Swift preprocessor flags, so it is no longer necessary to set these values ​​in "Other Swift Flags".

< /p>

The new setting is called "active compilation conditions", which provides top-level support for Swift equivalent preprocessor flags. You use it in exactly the same way as "other Swift flags", except that you don't need to use "- D" prefix (so it’s just a bit 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 of Preprocessor Macros pass to clang with the same prefix. (22457329)

enter image description here

You use the above settings as follows:

#if DEBUG
let accessToken = "DebugAccessToken"
#else
let accessToken = "ProductionAccessToken"
#endif

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4139 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.