How to use the @available attribute in Swift
Learn to make Swift code only available for certain language versions or platforms.
04 Aug 2025 · 3 min read
In 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.

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. For example:
@objcclass SomeClass: NSObject {}
or with arguments:
@available(iOS 15.0, *)func doSomething() {}
The @available attribute
The @available attribute lets us define the availability of functions, types, properties, and other declarations across specific platforms or Swift versions. This is especially useful when deprecating APIs, renaming methods, or introducing platform- or version-specific features.
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 the arguments of the @available attribute 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:
- unavailable - to indicate that a declaration isn't available on the specified platform
- introduced: <version number> - to specify the first version in which a declaration was introduced
- deprecated: <version number> - to specify the first version when a declaration was deprecated
- obsoleted: <version number> - to specify the first version when a declaration was obsoleted and can no longer be used
- message: <message> - to provide a message for the compiler to display when emitting a warning or error
- renamed: <new name> - to provide a new name
The <version number> consists of one to three positive integers separated by periods, for example:
@available(iOS, introduced: 17.0)struct Content {}
This makes Content available starting from iOS 17.0. It won't be available in earlier versions. If we want to use certain code paths on older versions as well, we can use an if #available check to conditionally execute code based on platform version at runtime:
if #available(iOS 17.0, *) {// Content is available here} else {// Fallback to some other code}
Combination of @available attributes
We can combine multiple availability declarations as follows:
@available(swift, introduced: 5.10)@available(iOS, introduced: 17.0)struct Content {}
This makes Content available only if both Swift version is at least 5.10 and the app is running on iOS 17.0 or later.
Shorthand syntax
In case that only an introduced argument is used, we can simplify using a shorthand syntax:
@available(swift 5.10)@available(iOS 17.0)struct Content {}
This is equivalent to the previous example, just written using the shorthand syntax.



Newsletter
Like to support my work?
Say hi
Related tags
Articles with related topics
Latest articles and tips



