The availability attribute
Developing our applications for the latest Operating System (OS) version gives us access to all 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. This was first introduced in Swift 2.
The availability attribute is only available when we use Swift on Apple platforms.
The availability block essentially lets us, if we are running the specified version of the operating system or higher, run this code or 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 that can be used with an if
or a guard
statement. The second way allows us to mark a method or type as available only on certain platforms.
The availability...