Swift availability API — @available & #available

@available and #available are very useful to handle on multiple platforms or to add proper depreciation error and warning in frameworks.

devCracker
3 min readOct 24, 2021
add deprecation warning or error in swift
Photo by Unjay Markiewicz on Unsplash

We often need to run the set of code for particular iOS version or platform, some use cases like handling the tab bar or navigation bar’s tint Color, when certain iOS release arrives in the market. Here in this article, let’s see, how we can use Swift’s #available and @available attribute for these kind of scenarios. We will cover different flavours of @available attribute and difference between @available and #available in this article.

By using @available attribute efficiently, we can make use of compiler’s capability to remind us by a warning or an error, when we decide to deprecate the certain functions or piece of code.

#available attribute in Swift

Making the piece of code available for particular iOS version & up or to a specific platform is part of development life in many use cases.

One example is UIDatePicker’s preferredDatePickerStyle, which is available only from iOS 13.4 and above. So, let’s say if our app supports from iOS 13.0 onwards and we need to set date picker style, compiler will throw error

compiler error for non available APIs

To fix this, we can use the Swift’s availability API #available as below.

Hence, the set of code which is covered within the #available attribute will be compiled/executed only on the specified version and above. We can also do the negation(if we have to execute the code below that particular version) as below.

if #available(iOS 13.4, *) { } else { }

Difference between @available and #available in Swift:

Swift has an another nice availability API @available attribute for the similar purpose, but there is a significant difference between #available and @available.

If we take a look on the above screenshot/use case, #available is used to control the piece of code with if condition, whereas it cant be used to mark the availability of a class or a method. In such a case, @available attribute helps us to mark the class or a method.

#available is to mark the piece of codes within the function for specific OS versions and platforms.

@available is to mark the class or a function for specific platform and OS versions.

@available deprecated, @available obsoleted, @available unavailable:

Let’s assume a use case, when you are building an open source framework or library and you want to inform the team who is using your’s lib that this function is deprecated from this version onwards. Yes, you can as below.

swift’s @available deprecated with message
swift’s @available deprecated with message

Okay, sometimes you need to force to throw the error when the team is using the deprecated function/class. You can obsolete that function as below and this time compiler will throw an error instead a warning.

swift’s @available obsoleted with message
swift’s @available obsoleted with message

@available’s unavailable is another beautiful one where if you need to inform the team that this function is no longer available and use another method instead.

swift’s @available’s unavailable message
swift’s @available’s unavailable message

Thats all for now and Many Thanks for reading!!!.

--

--

devCracker

professional mobile app developer | iOS | Android | GraphQL | Typescript | nodejs. Sports and musics fuels my mind to get back to development