Swift 2's availability feature should solve your problems. With availability, Swift builds in support for OS versioning and symbol availability testing.
First, set your deployment target in build settings to iOS 8.3 and your base SDK to Latest. If you use symbols (classes, methods, etc) in your project that are only available in operating systems newer than your deployment target (iOS 8.3), Xcode will display an error message with a fix-it on that line when you try to build and run your project.
See Checking API Availability in the Control Flow chapter of The Swift Programming Language book.
An availability check looks like this:
if #available (iOS 9, *) {
// use APIs only available on iOS 9 or later
} else {
// do nothing, don't show feature in UI, etc
}
Those are the basics. In the second part of your question, what you're looking for is a way to handle API availability on a larger scale. To do this, you can use another feature of Swift 2's availability feature, the @available
attribute. You can apply this attribute to any symbol definition–for example, a class–to mark that the definition of the symbol requires a certain minimum OS version.
Instead of using an availability check in every single place you use an iOS 9 API, you can just use the @available
attribute on an entire class. Then you only need to use an availability check at the place you use that class. For example:
@available(iOS 9, *)
class MyAwesomeiOSNineFeature {
var myCoolObject = UICooliOSNineThing()
func doAwesomeStuff() {
// lots of code that uses iOS 9 stuff, no availability check
}
func doOtherStuff() {
// lots of code that uses iOS 9 stuff, no availability check
}
}
// In another class that doesn't have an `@available` requirement:
if #available(iOS 9, *) {
let feature = MyAwesomeiOSNineFeature()
feature.doAwesomeStuff()
} else {
// don't show UI for new feature
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…