Ankit Goel Ankit Goel - 3 months ago 10
Swift Question

Difference between @available and #available in swift 2.0

Swift 2.0 allows availability checking by using

@available
or
#available
, but what is the difference between using
@available
and
#available
?

Answer

You can use if #available to run version-specific code in small blocks, like the following :

if #available(iOS 9, *) {
    // use UIStackView for example
} else {
    // do something else
}

But what if whole methods are off limits, Or perhaps even whole classes? Swift 2 has these scenarios covered too, using the @available attribute.

@available(iOS 9, *)
func useStackView() {
    // use UIStackView for example
}

More details : https://www.hackingwithswift.com/new-syntax-swift-2-availability-checking