What is the difference between #available and @available
Table of Contents
#available
and @available
are features related to the availability of APIs.
They are tools that mean to use together. Let's learn the difference and when to use them.
What is the difference between #available and @available
An availability condition (#available
) is used as a condition of an if
, while
, and guard
statement to query the availability of APIs at runtime.
An availability condition has the following form:
if #available(iOS 15, *) {
// statements to execute if the APIs are available
} else {
// fallback statements to execute if the APIs are unavailable
}
An @available
is a Declaration attribute that you apply to a class or method declaration to specified platforms that those classes or methods support.
Here is an example of @available
.
@available(iOS 16, *)
func newMethod() {
// A method that available on iOS 16 forward.
}
@available(iOS 16, *)
class NewClass {
// A class that available on iOS 16 forward.
}
You can easily support sarunw.com by checking out this sponsor.
Localization Buddy: Easiest way to localize and update App Store metadata.
Summary
#available
is a tool for API consumer. It helps us checking for the availability of features based on the platforms. This helps us cope with breaking changes in an API.
@available
is a tool for API creator. It is a tool that lets you specify availability to the class/method, so the consumer of that API knows how to use it.
Example
Let's say I have a class, OldClass
, with an instance method, oldMethod()
.
class OldClass {
func oldMethod() {
}
}
With the release of iOS 16, I want to add a new method that utilizes the iOS 16 feature. So, I create a new method, newMethod()
.
As, a creator, I mark my method with @available(iOS 16, *)
.
class OldClass {
@available(iOS 16, *)
func newMethod() {
// Method that utilize iOS 16 features.
}
func oldMethod() {
}
}
For users of my class, when they try to use newMethod()
on iOS lower than 16, they will get the following error.
let oldClass = OldClass()
oldClass.newMethod()
// 'newMethod()' is only available in iOS 16 or newer
They can handle this using #available
.
let oldClass = OldClass()
if #available(iOS 16, *) {
oldClass.newMethod()
} else {
// Fallback on earlier versions
}
Read more article about Swift or see all available topic
Enjoy the read?
If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.
Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.
If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.
Become a patron Buy me a coffee Tweet ShareHow to Reorder List rows in SwiftUI List
Learn how to enable/disable the reorder ability in SwiftUI List.
How to make Swift Enum conforms to Identifiable protocol
Learn how to make Swift Enum become Identifiable.