
Build 10x products in minutes by chatting with AI - beyond just a prototype.
Topics
What is the availability condition in Swift?
What is the difference between `@available` and #available in Swift?
What is Swift used for?
What are the two types Swift?
The attribute in Swift is a powerful feature that ensures your code works seamlessly across different platforms and versions. Whether you are supporting older iOS versions or leveraging new APIs, understanding this attribute is crucial for robust and future-proof app development.
This blog walks you through Swift usage, syntax, and best practices while showing how to write code that gracefully adapts to multiple platforms.
The attribute allows you to specify the platform availability of a particular method, function, or class. By using this attribute, you can indicate whether a piece of Swift code is available on a specific platform version or if it has been deprecated.
• Helps manage platform availability.
• Prevents compiler errors when using new APIs on unsupported platforms.
• Simplifies availability checks for ios versions and other platforms.
• Enhances app maintainability when targeting older versions and newer features simultaneously.
In Swift, apps often need to run code on multiple platforms and ios versions. The attribute ensures your app respects the minimum deployment target, avoiding crashes and deprecation warnings. Moreover, it helps developers adapt to the latest Swift version, manage api availability, and reduce compiler errors during development.
The attribute can be applied to various declarations like functions, methods, classes, or variables. Here's the syntax:
Swift
If a method was introduced in iOS 14, you can use the following:
Swift
Here:
• indicates the first version where this function is available.
• denotes other platforms are unaffected.
You can make your Swift code compatible with multiple platforms by listing them:
Swift
This ensures your app works across platform names like macOS and iOS.
When you need to execute certain code only on supported platform versions, availability checks help:
Use #available to verify platform availability dynamically:
Swift
This approach is useful for supporting older iOS versions without sacrificing new features.
When an API is deprecated, you can handle it gracefully:
Swift
This provides a note to developers and ensures the compiler issues a warning when the method is used.
With the rise of Mac Catalyst, your app might need features for both macOS and iOS:
Swift
This makes it easy to write code that adapts to newer versions of macOS and iOS.
Always align your usage of with your app’s deployment target to avoid unnecessary compiler errors.
When targeting older versions, provide alternative implementations to maintain functionality.
Regularly test your app on all target platforms to ensure smooth behavior and compatibility.
Use clear declaration messages to inform other developers about version-specific behavior.
When working with Objective-C code or third-party frameworks, the attribute helps ensure compatibility:
Swift
Leverage availability checks for platform-specific functionality:
Swift
This ensures your Swift code won’t trigger compiler errors for unsupported platforms.
The attribute in Swift is an indispensable tool for modern app development. By mastering it, you can efficiently manage platform availability, support older versions, and incorporate new APIs into your projects. Whether you are adapting to new iOS versions, maintaining cross-platform compatibility, or avoiding deprecation warnings, this feature is your go-to solution.
As you write code for multiple platforms, remember that clear availability checks and thoughtful usage of the @available attribute will keep your app running smoothly and future-proof.
@available@available@available@available@available@available@available@available@availableiOS 14.0*@available(platformName version, ...)@available(iOS 14.0, *)
func useNewFeature() {
// Code that uses a new API available in iOS 14
}@available(iOS 13.0, macOS 10.15, *)
func universalFeature() {
// Code for both iOS and macOS
}if #available(iOS 14, *) {
useNewFeature()
} else {
// Fallback code for older iOS versions
}@available@available@available@available@available@available@available@available(iOS, deprecated: 15.0, message: "Use updatedFunction instead.")
func deprecatedFunction() {
// Code that is no longer recommended
}@available(macCatalyst 14.0, *)
func macCatalystFeature() {
// Code exclusive to Mac Catalyst
}@available(iOS 12.0, *)
@objc func compatibleWithObjC() {
// Code callable from Objective-C
}@available(tvOS, unavailable)
func notForTvOS() {
// Code excluded from tvOS
}