@available and #available

Source: Internet
Author: User
Tags deprecated

Http://www.tuicool.com/articles/Jz2qAz7


In Swift 2.0, the concept of usability was introduced. For functions, classes, protocols, and so on, you can use @available to declare that these types of lifecycles depend on specific platforms and operating system versions. #available used in judgment statements (if, guard, while, etc.) to do different logic on different platforms. @available usage

@available placed in front of a function (method), class, or protocol. Indicates the platform and operating system to which these types are applicable. Look at one of the following examples:

@available (IOS 9, *)
func MyMethod () {
    //do something
}

@available (iOS 9, *) must contain at least 2 feature parameters, in which iOS 9 indicates that it must be available above the iOS 9 version. If you deploy a platform that includes IOS 8, the compiler will make an error after calling this method.

Another feature parameter: asterisk (*), which includes all platforms, currently has several platforms: IOS iosapplicationextension OS X osxapplicationextension watchOS Watchosapplicationextension tvOS tvosapplicationextension

Generally speaking, if there is no special case, use * to represent the whole platform.

@available (IOS 9, *) is a shorthand form. The full written form is @available (IOS, introduced=9.0). The introduced=9.0 parameter indicates that the specified platform (IOS) was introduced into the declaration starting from 9.0. Why it can be used in shorthand form. When only introduced such a parameter can be abbreviated to the above shorthand form. Similarly: @available (IOS 8.0, OS X 10.10, *) is also possible. Represents the availability of simultaneously on multiple platforms (IOS 8.0 and above; OS X 10.10 and above).

In addition, @available there are other parameters that can be used, respectively: deprecated= version number: From the specified platform to expire a version of the Declaration obsoleted= version number: From the specified platform to start a version of the discard (notice the difference, deprecated is still can continue to use, but is not recommended, obsoleted is the call will compile errors) the declaration message= information content: Give some additional information unavailable: Specify the platform is invalid renamed= new name: Rename the Declaration

The above parameters can refer to official documents #available

#available used in conditional statement code block, Judge different platform, do different logical processing, such as:

If #available (iOS 8, *) {
        //iOS 8 and above system run
}


guard #available (iOS 8, *) else {
    return//ios 8 The following system directly returns 
  }

Http://www.tuicool.com/articles/Jz2qAz7

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.