Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The @available attribute is a declaration attribute used to specify the platform, operating system version, or Swift language version required to access a specific declaration. It instructs the compiler to enforce API availability checks against the project’s minimum deployment target or the active Swift compiler version, emitting errors or warnings if a declaration is referenced in an unsupported context.

Syntax Forms

The attribute supports two primary syntactic forms: a shorthand list for basic version gating, and a detailed list for granular lifecycle management.

Shorthand Syntax

Used to declare the minimum required operating system versions across one or more platforms simultaneously.
@available(platform version, platform version, *)
  • platform: A recognized operating system platform identifier (e.g., iOS, macOS, tvOS, watchOS, visionOS, or macCatalyst). The swift language identifier is not permitted in shorthand syntax.
  • version: A version number formatted as major, major.minor, or major.minor.patch.
  • * (Wildcard): A mandatory token in the shorthand syntax. It indicates that the declaration is available on all other platforms not explicitly listed, inheriting the project’s minimum deployment target for those platforms.

Detailed Lifecycle Syntax

Used to define the complete lifecycle of a declaration on a specific platform or for a specific Swift language version, including deprecation and obsolescence.
@available(platform, introduced: version, deprecated: version, obsoleted: version, message: "string", renamed: "string")
  • platform: A specific OS platform identifier, the swift language identifier, or the * wildcard.
    • Swift Language Gating: To restrict a declaration by the Swift compiler version, the swift identifier must be used within this detailed syntax (e.g., @available(swift, introduced: 5.1) or @available(swift, obsoleted: 4.2)).
    • Wildcard Restriction: When using the * wildcard in the detailed syntax, it applies the modifiers unconditionally across all platforms. However, the * wildcard cannot be combined with versioned arguments (introduced, obsoleted, or deprecated: version). It can only be used with unversioned modifiers (e.g., @available(*, deprecated, message: "Use new API") or @available(*, unavailable)).

Lifecycle Modifiers

When using the detailed syntax, the compiler evaluates the following comma-separated arguments:
  • introduced: version: Defines the first version of the specified platform or Swift language where the declaration is accessible.
  • deprecated: version: Defines the version where the declaration remains accessible but is no longer recommended. The compiler emits a warning when the declaration is referenced. If the version is omitted (e.g., deprecated), it is treated as unconditionally deprecated on that platform.
  • obsoleted: version: Defines the version where the declaration is entirely removed from the API surface. The compiler emits an error if referenced. An obsoleted declaration cannot be used, even if the deployment target is higher than the introduced version.
  • message: "string": A string literal that the compiler appends to the diagnostic warning or error emitted by deprecated, obsoleted, or unavailable.
  • renamed: "string": A string literal providing the new name of the declaration. The compiler uses this to generate a Fix-It action in the IDE.

Absolute Unavailability

The attribute can explicitly forbid the use of a declaration on specific platforms using the unavailable argument.
@available(platform, unavailable, message: "string")
When unavailable is present, introduced, deprecated, and obsoleted cannot be used in the same attribute block.

Application Scope

The @available attribute can be applied to almost any top-level or nested declaration in the Swift Abstract Syntax Tree (AST), including:
  • Classes, Structs, and Enums
  • Protocols and Associated Types
  • Functions, Methods, and Initializers
  • Properties and Subscripts
  • Extensions
Multiple @available attributes can be stacked on a single declaration to define complex availability matrices across different platforms and language versions.
@available(macOS 12.0, *)
@available(iOS, introduced: 14.0, deprecated: 15.0, renamed: "newOperation()")
@available(swift, introduced: 5.5)
@available(watchOS, unavailable)
func legacyOperation() {
    // Implementation
}

Attribute vs. Condition

The @available attribute is strictly declarative and applies to API definitions. It is distinct from the #available and #unavailable conditions (which are compiler control statements). Conditions are evaluated at runtime within control flow statements (like if or guard) to safely execute code paths based on the host environment.
Master Swift with Deep Grasping Methodology!Learn More