With the dawn of Swift 3 on the horizon, there are a lot of changes coming down the line. There’s going to be a laundry list of breaking changes and future you is going to curse current you if you don’t get a feel for the brave new world. A handy feature that Objective-C developers have used for decades is version checking in preprocessor macros. You know, those fun
#ifndef’s that jut in and out of your code.
Well, Swift has its own way of directing the compiler toward relevant code. If you have a block of Swift that works on, say, version 3.0 but makes no sense to 2.2 you could wrap it in something like:
#if swift(>=3.0) print("Welcome to the future!”) #else print(“No Swift 3 Allowed!") #endif
This feature is available in Swift 2.2 (The current version as of writing this) and up. There are, however, a few caveats. You can only check the first two version components, so you can check for Swift 2.2 but not a minor point update such as 2.2.1.
You also cannot have
else if clauses. Which, isn’t a major issue at the moment but could be slightly more of a nuisance as Chris Lattner keeps painstakingly hand-making features and point releases in his secret lair.
Possibly most annoying, you can only compare versions with the greater than or equal comparator. Again, not the end of the world but hopefully it will be expanded upon in the future.