Swift 2 #available checking multiple platform versions

I haven't seen any documentation on #available and what the valid platform arguments are, nor how to combine them.


For example if we want to check for the existence of the new Contacts framework on iOS and Mac, and watchOS in the same code, how do we "OR" these requirements together, and what are the valid IDs for Mac OS and watchOS?


// How do we specify this for iOS *and* Mac *and* watchOS?
if #available(iOS 9, *) {
    setupWithContacts()
}

It looks like the valid platform symbols are iOS, OSX and watchOS:

@available(iOS 9, *)
@available(OSX 10.11, *)
@available(watchOS 2, *)
class ContactsData {
    let store = CNContactStore()
}

but without an ability to use #available with multiple platforms in one clause, this because rather clunky to use.

if #available(iOS 9.0, *), #available(OSX 10.11, *), #available(watchOS 2, *) {
    ContactsData()
}


...this results in the playground console output:


Playground execution failed: /var/folders/0v/626mc7q94fnd6zmh6z6b8qm40000gq/T/./lldb/40279/playground169.swift:14:5: error: 'ContactsData' is only available on iOS 9 or newer

ContactsData()

^

/var/folders/0v/626mc7q94fnd6zmh6z6b8qm40000gq/T/./lldb/40279/playground169.swift:14:5: note: guard with version check

ContactsData()

^

Accepted Reply

This is documented in the Swift iBook, and in HTML version that's in the Swift section of the Resources section of the developer web site, not to mention various WWDC videos from the last week.


You combine tests like this:


if #available(iOS 9, OSX 10.10, *)

Replies

This is documented in the Swift iBook, and in HTML version that's in the Swift section of the Resources section of the developer web site, not to mention various WWDC videos from the last week.


You combine tests like this:


if #available(iOS 9, OSX 10.10, *)

Thanks... I didn't spot it as search was not working in iBooks on iOS 9 beta which is on the device I use for the docs 😟