Learning iOS Development

Mastering #available Attribute in Swift

Best Practices with Examples

Shashank Thakur
Mobile App Development Publication
3 min readSep 29, 2023

--

Mastering #available Attribute in Swift
Photo by James Lee on Unsplash

Swift, Apple’s modern programming language, continues to evolve with each new release, bringing developers a host of new features and enhancements. One such feature is the #available attribute, which allows developers to write code that gracefully handles changes in APIs and features across different iOS or macOS versions. In this blog post, we'll explore the best practices for using the #available attribute in Swift through practical examples, ensuring your applications remain compatible and maintainable across various platform versions.

Understanding the #available Attribute

The #available attribute is a conditional compilation attribute in Swift that enables you to check the availability of APIs, features, or platform versions at compile-time. It allows you to write code paths that are executed only when certain conditions are met, ensuring that your app works correctly on different iOS or macOS versions.

Basic Syntax

Here’s the basic syntax of the #available attribute:

if #available(platformName version, ...) {
// Code that will execute on supported platforms and versions
} else {
// Code that will execute on unsupported platforms and versions
}

Now, let’s dive into best practices with real-world examples:

Best Practices for Using #available

1. Always Check Availability

Before using any new API or feature, check its availability using #available. This ensures that your code won't crash or behave unexpectedly on older platforms that lack support for the feature.

if #available(iOS 15, macOS 12, *) {
// Use the latest API or feature
} else {
// Provide an alternative or gracefully handle the absence of the feature
}

In this example, we’re checking if the code runs on iOS 15 or macOS 12. If not, we provide an alternative path.

2. Use the Lowest Deployment Target as the Default

Specify the lowest deployment target your app supports when checking for availability. This ensures that your code handles older versions appropriately by providing alternative implementations.

if #available(iOS 15, *) {
// Use the latest API on iOS 15 or higher
} else {
// Provide alternative code for older iOS versions
}

Here, if the app runs on iOS versions earlier than 15, it falls back to the alternative code.

3. Consider Using Feature Availability

In addition to checking for platform versions, consider using feature availability checks, especially if you’re using specific features that may not be available on all devices, even within the same iOS version.

if #available(iOS 15.0, *, iOSApplicationExtension 15.0) {
// Use features only available on specific iOS devices or extensions
} else {
// Provide alternatives or handle feature absence gracefully
}

This example ensures that the code runs only on iOS 15.0 or later and includes a feature-specific check.

4. Keep Code Clean

Avoid cluttering your code with excessive availability checks. Instead, centralize these checks in a single location, such as a configuration file or dedicated utility functions. This keeps your codebase clean and easier to maintain.

5. Leverage Swift’s Syntax Sugar

Swift provides a convenient syntax sugar for use #available with a single condition. You can use the @available attribute in combination with functions or computed properties to create clean and reusable checks.

@available(iOS 15, *)
func useLatestFeature() {
// Use the latest feature on iOS 15 or higher
}

// Elsewhere in your code
useLatestFeature() // This function will only execute on iOS 15 or higher

This example demonstrates the use of @available to encapsulate availability checks in a function.

6. Test on Multiple Devices and Versions

Testing your app on a variety of devices and iOS or macOS versions is crucial. While #available helps ensure your code won't crash, and testing reveals how your app behaves under different conditions and whether you've handled feature absence gracefully.

Conclusion

The #available attribute in Swift is a powerful tool for handling platform and feature availability in your iOS or macOS applications. By following these best practices and exploring practical examples, you can write code that gracefully adapts to different platform versions, provides a consistent user experience, and remains maintainable as your app evolves. Ultimately, using #available the right way contributes to the overall stability and success of your Swift projects.

--

--