TL;DR? > Go to 3. Solution
1. Preprocessing in Swift
According to Apple documentation on preprocessing directives:
The Swift compiler does not include a preprocessor. Instead, it takes
advantage of compile-time attributes, build configurations, and
language features to accomplish the same functionality. For this
reason, preprocessor directives are not imported in Swift.
That is why you have an error when trying to use __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0
which is a C preprocessing directive. With swift you just can't use #if
with operators such as <
. All you can do is:
#if [build configuration]
or with conditionals:
#if [build configuration] && ![build configuration]
2. Conditional compiling
Again from the same documentation:
Build configurations include the literal true and false values,
command line flags, and the platform-testing functions listed in the
table below. You can specify command line flags using -D <#flag#>.
true
and false
: Won't help us
- platform-testing functions:
os(iOS)
or arch(arm64)
> won't help you, searched a bit, can't figure where they are defined. (in compiler itself maybe?)
- command line flags: Here we go, that's the only option left that you can use...
3. Solution
Feels a bit like a workaround, but does the job:
Now for example, you can use #if iOSVersionMinRequired7
instead of __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0
, assuming, of course that your target is iOS7.
That basically is the same than changing your iOS deployment target version in your project, just less convenient...
Of course you can to Multiple Build configurations with related schemes depending on your iOS versions targets.
Apple will surely improve this, maybe with some built in function like os()
...
__IPHONE_OS_VERSION_MIN_REQUIRED
or similar is available (yet?), but you could just use your own build configuration variables. or maybe trick using "Simple macros" imported into swift via a .h header. – Cystoid__IPHONE_OS_VERSION_MIN_REQUIRED
is built-in. If you type it into one of your.swift
files in Xcode and command-click on it, Xcode takes you to its declaration:var __IPHONE_OS_VERSION_MIN_REQUIRED: CInt { get }
. I do not want to specify superfluous command line flags. I want to use the built-in one so that when I change the iOS deployment target in Xcode to iOS 8 (and up), Xcode compiles my code correctly. – Sollars