Swift how to use PREPROCESSOR Flags like if DEBUG to implement API keys?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Swift, use #if DEBUG to conditionally compile code for different build configurations. This is commonly used to switch between development and production API keys, base URLs, and logging levels. Unlike C preprocessor macros, Swift compilation conditions are checked by the compiler and do not perform text substitution. Define custom flags in Xcode under Build Settings > Swift Compiler > Active Compilation Conditions, or pass -D FLAG_NAME to swiftc. The DEBUG flag is predefined for debug builds in Xcode projects.
Basic #if DEBUG Usage
Code inside the inactive branch is not compiled at all — it does not add to your binary size or affect runtime performance.
Setting Up Custom Flags in Xcode
- Select your target in Xcode
- Go to Build Settings
- Search for "Active Compilation Conditions"
- Add flags per configuration:
- Debug:
DEBUG - Release:
RELEASE - Custom:
STAGING,BETA, etc.
In your Package.swift (for SPM):
Secure API Key Management
xcconfig-Based Approach (Recommended)
Logging with Conditional Compilation
OS and Platform Checks
Common Pitfalls
- Hardcoding API keys directly in source files tracked by git: Even with
#if DEBUG, both keys exist in your source code. If the repository is public or compromised, production keys are exposed. Use.xcconfigfiles or aSecrets.swiftfile added to.gitignoreto keep keys out of version control. - Assuming
DEBUGis always defined:DEBUGis only predefined in Xcode debug configurations. If you create a custom build configuration or use SPM without explicit settings,DEBUGmay not be set. Always verify your build settings include the flag, or define it explicitly in Active Compilation Conditions. - Using
#iffor runtime branching instead of compile-time:#if DEBUGis evaluated at compile time — the inactive branch is completely excluded from the binary. Do not confuse this with runtime checks likeif isDebug { }. If you need runtime configuration switching (e.g., a settings toggle), use a regular variable instead of a compilation condition. - Escaping URLs in xcconfig files: xcconfig files treat
//as a comment delimiter. URLs likehttps://api.example.comare truncated at//. Usehttps:$(/)$(/)api.example.comto escape the slashes, or store only the domain and construct the URL in code. - Not securing keys in CI/CD pipelines: Even with
.gitignore, CI/CD systems need access to production keys. Store them as encrypted environment variables or secrets (GitHub Secrets, Xcode Cloud Environment Variables) and inject them during the build process, not in committed files.
Summary
- Use
#if DEBUG/#else/#endifto switch between development and production API keys at compile time - Define custom flags in Xcode Build Settings > Active Compilation Conditions
- Use
.xcconfigfiles per environment for scalable, git-friendly configuration management - Never commit production API keys to source control — use
.gitignore, xcconfig files, or CI/CD secrets - Remember that
#ifis compile-time only — the inactive branch is not included in the binary
Related reading
- Sync data between Android App and webserver
- Synchronization in distributed processes
- Synchronize actions in a distributed system
- Synchronous and Asynchronous data transmission between client and server
- Swift how to wrap completion into an async/await?
- Swift Images change to wrong images while scrolling after async image loading to a UITableViewCell
- Swift performSegueWithIdentifier not working
- Swift print vs println vs NSLog

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.