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 let 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 second way allows us to mark a method or type as available only on certain platforms.
The availability
attribute accepts up to five comma-separated arguments that allow us to define the minimum version of the operating system or application extension...