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