Swift API Availability Checking

Source: Internet
Author: User

In the process of development, it is inevitable that we will use the new API, if not properly handled, for less than this version of the device running the application may crash, in order to prevent this situation, we need at runtime to determine whether the API is available.

IOS9 introduced a new view class, called Uistackview, which is a view container (divided into horizontal and vertical layouts), and unlike other view, the view automatically manages the layout of the internal view without the need for automatic layout of the code. Also careful you will find that in the lower right corner of the storyboard file is a button called stack (and Align,pin,resolve AutoLayout issues tied):


If you are interested in it, you can click here.

Since this class only appears after iOS9, we can use this method to determine if the class is available in order to take into account the iOS9 of the previous device.

if (nsclassfromstring ("uistackview") = nil) {    //Uistackview is available} else {    //Uistackview does not exist.}

By passing the class name to the function nsclassfromstring, determine whether the current environment supports the class based on the return value. Returning nil means that the class does not exist, otherwise the class is available. One of the bad things about this method is that the parameter is a string and may have the wrong class name, such as nsclassfromstring ("Uistackview").


After Swift 2, Apple introduced API availability Checking, if a new API is used in the Xcode7 project, the compiler will determine whether the class, method, or property is available based on the user-specified deployment target. If not available, a compilation error is given, and you can then fix the error as prompted. Here is a replacement for the above code.

If #available (IOS 9.0, *) {    //Uistackview is available} else {    //Fallback on earlier versions}

Reference: Http://www.hackingwithswift.com/new-syntax-swift-2-availability-checking

Reference: https://developer.apple.com/videos/wwdc/2015/?id=106



Swift API Availability Checking

Related Article

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.