What's the Swift equivalent of Objective-C's "#ifdef __IPHONE_11_0"? What's the Swift equivalent of Objective-C's "#ifdef __IPHONE_11_0"? xcode xcode

What's the Swift equivalent of Objective-C's "#ifdef __IPHONE_11_0"?


The iOS 11 SDK comes with Swift 3.2 (or Swift 4), so you can use a Swift version check to accomplish the same thing:

#if swift(>=3.2)    if #available(iOS 11.0, *) {            }#endif


This is the solution suggested by Apple:

if #available(iOS 11.0, *) {    // iOS 11 specific stuff here} else {    // non iOS 11 stuff here}

Please refer to this resource (watch video on mark 6:50 for more details)


If you want to put the condition outside of the function, you could do it like below.

@available(iOS 11.0, *)func functionName() { // function contents}