Swift version: 5.6
Swift gives us the #if swift
build configuration option, which lets you compile certain code only if a specific version of the Swift compiler is detected. This is particularly useful for libraries that need to support multiple incompatible versions of Swift at the same time, because only one version of their code will ever be compiled.
In the example below, the print()
code will display one of two messages depending on whether you're using an old version of Swift:
#if swift(>=5.0)
print("Running Swift 5.0 or later")
#else
print("Running old Swift")
#endif
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 7.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.