What does @available mean?

Hi,


I'm aware that there's a keyword @available which is supposed to be used to determine what version of iOS is available, but I can't find the documentation for it, only examples. I'd prefer to know exactly how it works rather than rely on reading an example and guessing. Where is this documented?


If I write @available(iOS 11, *) does that evaluate true for iOS 11 and later, or just iOS 11? What does the "*" mean? What else could I have typed instead of "iOS 11"? Is "iOS 11" a keyword, or an arbitrary string, or what?


Thanks,

Frank

This keywork or attribute is specific to swift and yes it means the function will be avaialble for iOS 11 and up & available to all other levels of Apple Operating Systems & Exrensions for Apple TV, macOS, Apple Watch etc ... The counter part to this keyword is also #available(*,*,*)


Refer to Using Swift with Cocoa & Objective-C (Swift 4) - Adopting Cocoa Design Patterns page 121

I think you have it backwards... @available is Objective-C, the other way must be Swift.


At least, I typed that into an Objective-C file and it compiled.


I do not use Swift.

Well you try using it in objective-c and tell me how it goes 🙂

Where do I get this book you mention? Is it a free book from Apple or something I have to buy?

I did. It worked.


I've been programming Objective-C since 2009. I've seen lots of other @ directives, like @synchronized, or @autorelease, but never one with a # sign.


The # sign is used for preprocessor defines in Objective-C, hence I'd be surprised to see it used anywhere else.


Frank

The OP is correct. The @available mechanism was added to the clang compiler for Objective C with Xcode 9. It is modelled on the Swift #available syntax, hence the similarity of the parameters.


It's probably difficult to find documentation about this, other than the Xcode release notes where it was announced:


https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html


(under the heading "Apple LLVM Compiler and Low-Level Tools")


and perhaps on the clang.llvm.org website. However, the meaning of the parameters is the same as documented for Swift #available:


— Each parameter except the last indicates the minimum version of an OS/platform. The version number is in the form X, X.Y or X.Y.Z.


— The last "*" indicates that the code is also for all other platforms beyond the ones listed in the test.


So @available (iOS 11, *) indicates code for iOS 11 and above (so, including 11.0.1 and 11.1, etc), and for all non-iOS platforms.

What does @available mean?
 
 
Q