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.

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.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
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. For example:

@objc
class 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:

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.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

testing

swift

ios

Parameterized tests and test organization with Swift Testing

Learn how to cover multiple inputs with parameterized tests and organize a growing Swift Testing test suite.

21 Sep 2026 · 7 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum