我知道我可以找到我正在运行的 Swift 版本恢复到终端并输入:
xcrun swift --version
Swift version 1.1 (swift-600.0.57.4)
Target: x86_64-apple-darwin13.4.0
此外,我一直在阅读有关 Preprocessor Macros 的信息在 Swift 中,但没有找到 Swift 版本常量。
随着 Swift 1.2 的临近,最好标记仅在 Swift 1.1(Xcode 最高 6.2)上运行的旧代码或需要 Xcode 6.3(Swift 1.2)的新代码
注意:我也可以使用 system() 来做类似的事情:
system("xcrun swift --version | grep version > somefile.txt")
然后打开 somefile.txt,但更喜欢一些更简单的解决方案
最佳答案
您可以使用条件编译指令来测试用于构建项目的特定 Swift 版本:
#if swift(>=5.0)
print("Hello, Swift 5!")
#elseif swift(>=4.0)
print("Hello, Swift 4!")
#elseif swift(>=3.0)
print("Hello, Swift 3!")
#elseif swift(>=2.2)
print("Hello, Swift 2.2!")
#elseif swift(>=2.1)
print("Hello, Swift 2.1!")
#endif
关于ios - 如何以编程方式找到 Swift 的版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27242690/