Logo for tanaschita.com

How to use the @available attribute in Swift

Learn to make Swift code only available for certain language versions or platforms.

06 Feb 2023 · 2 min read

With Swift, we have the possibility to add attributes to our code to give the compiler more information.

One of those attributes is @available which we can use to make our code only available for certain Swift language versions or platforms.

Let's take a look at how to do that.

Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Attributes in Swift

In general, an attribute in Swift is always specified with the @ symbol followed by the attribute's name and optionally its arguments enclosed in parentheses:

@name
@name(arguments)

The @available attribute

The @available attribute always has two or more arguments, separated by a comma, for example:

@available(*, unavailable, renamed: "updateCategories")
func update() {
}

In the above example, we declare the update method as not available on any platform because it was renamed to updateCategories.

Calling the update method will now fail with a compiler error:

update() has been renamed to updateCategories()

Arguments of @available attribute

Let's look at each argument we saw in the example in more detail.

The first argument is always a platform or language name, for example iOS, iOSApplicationExtension, swift. Alternatively, we can use an asterisk * to indicate that the availability applies to all platforms.

The remaining arguments can appear in any order:

The <version number> consists of one to three positive integers separated by periods, for example:

@available(iOS, introduced: 15.1)
struct Content {
}

Combination of @available attributes

We can combine multiple availability declarations as follows:

@available(swift, introduced: 4.0.2)
@available(iOS, introduced: 15.1.1)
struct Content {
}

Shorthand syntax

In case that only an introduced argument is used like we saw in the example above, we have the possibility to use a shorthand syntax as follows:

@available(swift 4.0.2)
@available(iOS 15.1.1, *)
struct Content {
}
Sponsorship logo
Preparing for a technical iOS job interview - updated for iOS 17
Check out my book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift, SwiftUI, Combine, HTTP Networking, Authentication, SwiftData & Core Data, Concurrency with async/await, Security, Automated Testing, Machine Learning and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

swiftdata

swift

ios

How to define one-to-many relationships in SwiftData

Learn to use SwiftData's @Relationship macro.

19 Feb 2024 · 4 min read

Latest articles and tips

© 2024 tanaschita.com

Privacy policy

Impressum