#if targetEnvironment(simulator) for Swift 4.1+ to conditionally compile code for the simulator. For older versions, check architecture with #if (arch(i386) || arch(x86_64)) && os(iOS). Alternatively, add a custom -D IOS_SIMULATOR flag in Other Swift Flags for the simulator SDK.Why this happens
Swift does not expose a built-in isSimulator property at compile time. The preprocessor macros from Objective-C (TARGET_IPHONE_SIMULATOR) are not directly available in Swift source files. Developers need a way to include or exclude code paths—such as different imports, stub implementations, or test hooks—depending on whether the target is a physical device or a simulator, and this decision must happen at compile time, not runtime.
How to fix it
1. Use targetEnvironment(simulator) (Swift 4.1 and later)
This is the canonical, Apple-recommended way. It works for iOS, watchOS, and tvOS simulators without checking architectures.
#if targetEnvironment(simulator)
// Simulator-only code
import SimulatorOnlyFramework
let isSim = true
#else
// Device-only code
import DeviceOnlyFramework
let isSim = false
#endif
The condition evaluates during compilation, so the unused branch is completely stripped. No runtime overhead, no unreachable-code warnings.
2. Legacy architecture check (pre-Swift 4.1)
If you maintain a codebase stuck on Swift 4.0 or earlier, combine architecture and OS checks:
#if (arch(i386) || arch(x86_64)) && os(iOS)
// iOS simulator (32-bit or 64-bit Intel)
#else
// Physical device or other platforms
#endif
For watchOS or tvOS simulators, swap os(iOS) for os(watchOS) or os(tvOS).
3. Custom compiler flag per SDK (works on any Swift version)
Some teams prefer an explicit flag they control. In Xcode:
- Select the project → Build Settings.
- Search Other Swift Flags (
OTHER_SWIFT_FLAGS). - Expand the row, click the
+next to Any iOS Simulator SDK. - Add
-D IOS_SIMULATOR.
Now the flag is only defined when building for a simulator:
#if IOS_SIMULATOR
print("Running in simulator")
#else
print("Running on device")
#endif
You can wrap it in a UIDevice extension for a clean API:
extension UIDevice {
var isSimulator: Bool {
#if IOS_SIMULATOR
return true
#else
return false
#endif
}
}
// Usage: UIDevice.current.isSimulator
4. Runtime fallback (when compile-time isn’t possible)
If you only need the value at runtime—for analytics, logging, or UI—inspect the Darwin constant:
import Darwin
var isSimulator: Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
Or, without the compile-time branch:
func isRunningInSimulator() -> Bool {
#if targetEnvironment(simulator)
return true
#else
return false
#endif
}
Avoid TARGET_OS_SIMULATOR in Swift; it requires importing C headers and is less idiomatic.
How to prevent it
- Default to
#if targetEnvironment(simulator)for all new code. - Keep custom flags (
-D IOS_SIMULATOR) only if you have legacy modules that already depend on them. - Never use runtime checks (
UIDevice.current.model,ProcessInfo.processInfo.environment) to gate compile-time-only constructs like imports or@availableattributes. - Document the chosen pattern in your team’s coding guidelines so new contributors don’t reintroduce architecture checks.
FAQ
Can I use #if targetEnvironment(simulator) inside a Swift package manifest?
No. Package.swift is evaluated by the Swift Package Manager, which does not expose targetEnvironment. Use .when(platforms: [.iOS]) or separate targets for simulator-specific sources.
Does targetEnvironment(simulator) work for Mac Catalyst builds?
No. Mac Catalyst apps run on macOS, not in the iOS simulator. The condition returns false for Catalyst. Use #if targetEnvironment(macCatalyst) to detect that case.
Why do I get “unreachable code” warnings with a runtime if?
Because the compiler replaces #if branches with 0 or 1 literals. A regular if isSimulator { … } where isSimulator is a compile-time constant triggers the warning. Keep the #if at the top level or wrap the constant in a function so the branch is not evaluated until runtime.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.