available()

The available() function provides the same functionality as the if #available language syntax in Swift, but makes it accessible to all four languages.

Syntax:

method available(params platforms: array of String): Boolean;
bool available(params String[] platforms);
func available(platforms: String...) -> Bool

available() can be used to protect code that will only work on specific newer versions of the platform, by specifying the minimum platform version required. Like if #available, it serves two purposes at once:

  1. It returns a boolean that can be used to check the platform version at runtime.
  2. In combination with an if statement, it will tell the compiler to automatically omit any deployment target warnings in the code that is protected by the if available clause.

For the second part to have affect, the call to assigned() must be the only condition in the if statement, or the first of multiple conditions combined with the logical and (Oxygene) or && (C# and Swift) operator.

available() expects a list of strings as parameter. Each string must be a platform name, followed by a space and a version number. The version number can consist of one, two or three parts (i.e. "iOS 9", "iOS 9.1" or "iOS 9.0.1"). Platform names can be "OS X", "iOS", "watchOS" and "tvOS", and are not case sensitive.

If a platform is not represented in the list, available() will return for this platform, unless the list of platforms is terminated with a wildcard ("*") string. For example, the code below checks for a specific iOS and OS X version. When compiled for run on watchOS or tvOS, the code inside the if statement will run regardless of version, because these two platforms are being covered by *.

Please refer to the Deployment Targets topic for further discussion on this.

Example

if available("iOS 9.0", "OS X 10.11", "*") then begin
  // code that requires the new OS.
end;
if (available("iOS 9.0", "OS X 10.11", "*"))
{
  // code that requires the new OS.
}
if available("iOS 9.0", "OS X 10.11", "*") {
  // code that requires the new OS.
}

The code inside the if statement will only be executed if the platform requirements (in this case, iOS 9 or later, or OSX 10.11 or later) are met. Any deployment target warnings the code inside the if statement would normally emit will be suppressed, because the compiler "knows" that the code will not run on the lower deployment targets.

Note that in Swift,

if available("iOS 9.0", "OS X 10.11") {

is equivalent to the Swift-specific syntax:

if #available(iOS 9.0, OS X 10.11) {

Under the hood, available() calls the __ElementsCocoaVersionAtLeast() helper function in libToffee.fx.

Cocoa Only

  • available() is currently supported on Cocoa and Cooper/Android only.