Swift: iOS deployment command line flag

How to check iOS deployment target in Swift conditional compilation instruction?

I tried the following:

#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 // some code here #else // other code here #endif 

But the first expression causes a compilation error:

 Expected '&&' or '||' expression 
+8
ios swift conditional-compilation deployment-target
source share
4 answers

TL; DR? > Go to 3. Solution

1. Preprocessing in Swift

According to Apple's Pre-Processing Guidelines Documentation :

The Swift compiler does not include a preprocessor. Instead, it takes the advantage of compilation attributes, building configurations, and to implement the same functionality. For this reason, preprocessor directives are not imported into Swift.

This is why you have an error while trying to use __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 , which is the C preprocessing directive. With swift, you simply cannot use #if with operators such as < . All you can do is:

 #if [build configuration] 

or with symbols:

 #if [build configuration] && ![build configuration] 

2. Conditional compilation

Again from the same documentation :

Assembly configurations include true and false literals, command line flags, and platform testing functions, as listed in the table below. You can specify command line flags using -D <#flag #>.

  • true and false : will not help us
  • platform testing functions : os(iOS) or arch(arm64) > will not help you, search a little, they cannot determine where they are defined. (maybe in the compiler?)
  • command line flags . Here we say that the only option you can use ...

3. Decision

Feels like a workaround, but does the job: Other swift flags

Now, for example, you can use #if iOSVersionMinRequired7 instead of __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_7_0 , assuming, of course, that your goal is iOS7.

Basically, it’s the same as changing the target version of the iOS deployment in your project, which is less convenient ... Of course, you can use several build configurations with related circuits depending on the goals of your version of iOS.

Apple will certainly improve this, perhaps with some built-in function like os() ...

+11
source share

Tested in Swift 2.2

Speaking of Deployment Target , do you mean iOS version or App Target ? Below I provide a solution if you have several versions of the application (free application, paid application ...), so that you use different target applications.

You can customize your build settings:
1. go to your project / select your goal / Build Settings / search for custom flags
2. for your chosen goal, set your own flag using the -D prefix (no spaces) for both Debug and Release
3. take steps for each of your goals

enter image description here

To distinguish between goals, you can do something like this:

 var target: String { var _target = "" #if BANANA _target = "Banana" #elseif MELONA _target = "Melona" #else _target = "Kiwi" #endif return _target } override func viewDidLoad() { super.viewDidLoad() print("Hello, this is target: \(target)" } 
+6
source share

You cannot do this in a conditional compilation statement. Complex macros, as Apple calls them, are not supported in Swift. Generics and types do the same thing, in their opinion, with better results. (Here is the link they posted https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_13 )

Here the function I came across does the exact same thing (and obviously just replaces the returned strings with what is useful to you, like a boolean or just the option itself):

 func checkVersion(ref : String) -> String { let sys = UIDevice.currentDevice().systemVersion switch sys.compare(ref, options: NSStringCompareOptions.NumericSearch, range: nil, locale: nil) { case .OrderedAscending: return ("\(ref) is greater than \(sys)") case .OrderedDescending: return ("\(ref) is less than \(sys)") case .OrderedSame: return ("\(ref) is the same as \(sys)") } } // Usage checkVersion("7.0") // Gives "7.0 is less than 8.0" checkVersion("8.0") // Gives "8.0 is the same as 8.0" checkVersion("8.2.5") // Gives "8.2.5 is greater than 8.0" 
0
source share

I know that your question has been here for a while, but in case someone is still looking for an answer, he should know that, starting with Swift 2.0, you can do something like this:

 if #available(iOS 8, *) { // iOS 8+ code } else { // older iOS code } 

Learn more about this here .

0
source share

All Articles