我知道我可以找到我现在正在运行的 Swift 版本,恢复到终端并输入:
xcrun swift --version
Swift version 1.1 (swift-600.0.57.4)
Target: x86_64-apple-darwin13.4.0
另外,我一直在阅读有关预处理器宏 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-XID_20在 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)