The availability attribute

Using the latest SDK gives us access to all of the latest features for the platform that we are developing for; however, there are times when we want to also target older platforms. Swift allows us to use the availability attribute to safely wrap code to run only when the correct version of the operating system is available. The availability was first introduced in Swift 2.

The availability blocks essentially lets us say, "If we are running the specified version of the operating system or higher, run this code. Otherwise, run some other code." There are two ways in which we can use the availability attribute. The first way allows us to execute a specific block of code and can be used with an if or guard statement. The ...

Get Mastering Swift 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.