Swift 言語バージョンをプログラムで取得する

### ・ Get Swift language version programmatically

https://forums.swift.org/t/get-swift-language-version-programmatically/11771/9

`#languageVersion` や `#swiftVersion` が 実装されてないので、`#if` で愚直に判定します。



### ・ #if

```swift
#if swift(>=6.2)
let swiftVersion = "6.2"
#elseif swift(>=6.1)
let swiftVersion = "6.1"
#elseif swift(>=6.0)
let swiftVersion = "6.0"
#elseif swift(>=5.10)
let swiftVersion = "5.10"
#elseif swift(>=5.9.2)
let swiftVersion = "5.9.2"
#elseif swift(>=5.9.2)
let swiftVersion = "5.9.2"
#elseif swift(>=5.9)
let swiftVersion = "5.9"
#elseif swift(>=5.8.1)
let swiftVersion = "5.8.1"
#elseif swift(>=5.8)
let swiftVersion = "5.8"
#elseif swift(>=5.7.2)
let swiftVersion = "5.7.2"
#elseif swift(>=5.7.1)
let swiftVersion = "5.7.1"
#elseif swift(>=5.7)
let swiftVersion = "5.7"
#elseif swift(>=5.6.1)
let swiftVersion = "5.6.1"
#elseif swift(>=5.6)
let swiftVersion = "5.6"
#elseif swift(>=5.5.2)
let swiftVersion = "5.5.2"
#elseif swift(>=5.5.1)
let swiftVersion = "5.5.1"
#elseif swift(>=5.5)
let swiftVersion = "5.5"
#elseif swift(>=5.4.2)
let swiftVersion = "5.4.2"
#elseif swift(>=5.4)
let swiftVersion = "5.4"
#elseif swift(>=5.3.2)
let swiftVersion = "5.3.2"
#elseif swift(>=5.3.2)
let swiftVersion = "5.3.2"
#elseif swift(>=5.3.1)
let swiftVersion = "5.3.1"
#elseif swift(>=5.3)
let swiftVersion = "5.3"
#elseif swift(>=5.2.4)
let swiftVersion = "5.2.4"
#elseif swift(>=5.2.2)
let swiftVersion = "5.2.2"
#elseif swift(>=5.2)
let swiftVersion = "5.2"
#elseif swift(>=5.1.3)
let swiftVersion = "5.1.3"
#elseif swift(>=5.1.2)
let swiftVersion = "5.1.2"
#elseif swift(>=5.1)
let swiftVersion = "5.1"
#elseif swift(>=5.0.1)
let swiftVersion = "5.0.1"
#elseif swift(>=5.0)
let swiftVersion = "5.0"
#elseif swift(>=4.2.1)
let swiftVersion = "4.2.1"
#elseif swift(>=4.2)
let swiftVersion = "4.2"
#elseif swift(>=4.1.2)
let swiftVersion = "4.1.2"
#elseif swift(>=4.1)
let swiftVersion = "4.1"
#elseif swift(>=4.0.3)
let swiftVersion = "4.0.3"
#elseif swift(>=4.0.2)
let swiftVersion = "4.0.2"
#elseif swift(>=4.0)
let swiftVersion = "4.0"
#elseif swift(>=3.1)
let swiftVersion = "3.1"
#elseif swift(>=3.0.2)
let swiftVersion = "3.0.2"
#elseif swift(>=3.0.1)
let swiftVersion = "3.0.1"
#elseif swift(>=3.0)
let swiftVersion = "3.0"
#elseif swift(>=2.2)
let swiftVersion = "2.2"
#elseif swift(>=2.1)
let swiftVersion = "2.1"
#elseif swift(>=2.0)
let swiftVersion = "2.0"
#elseif swift(>=1.2)
let swiftVersion = "1.2"
#elseif swift(>=1.1)
let swiftVersion = "1.1"
#elseif swift(>=1.0)
let swiftVersion = "1.0"
#else
let swiftVersion = "unknown"
#endif

print(swiftVersion)
//5.8.1

let versionArray = swiftVersion.split(separator: ".").map{ Int($0)! }
print(versionArray)
//[5, 8, 1]
```



### ・ print（リリース日）

```swift
#if swift(>=6.2)
print("Swift 6.2 (development)")
#elseif swift(>=6.1)
print("Swift 6.1 (2025/3/31)")
#elseif swift(>=6.0)
print("Swift 6.0 (2024/9/16)")
#elseif swift(>=5.10)
print("Swift 5.10 (2024/3/5)")
#elseif swift(>=5.9.2)
print("Swift 5.9.2 (2024/1/8)")
#elseif swift(>=5.9.2)
print("Swift 5.9.2 (2023/12/11)")
#elseif swift(>=5.9)
print("Swift 5.9 (2023/9/18)")
#elseif swift(>=5.8.1)
print("Swift 5.8.1 (2023/6/1)")
#elseif swift(>=5.8)
print("Swift 5.8 (2023/3/30)")
#elseif swift(>=5.7.2)
print("Swift 5.7.2 (2022/12/13)")
#elseif swift(>=5.7.1)
print("Swift 5.7.1 (2022/11/1)")
#elseif swift(>=5.7)
print("Swift 5.7 (2022/9/12)")
#elseif swift(>=5.6.1)
print("Swift 5.6.1 (2022/5/16)")
#elseif swift(>=5.6)
print("Swift 5.6 (2022/3/14)")
#elseif swift(>=5.5.2)
print("Swift 5.5.2 (2021/12/13)")
#elseif swift(>=5.5.1)
print("Swift 5.5.1 (2021/10/25)")
#elseif swift(>=5.5)
print("Swift 5.5 (2021/9/20)")
#elseif swift(>=5.4.2)
print("Swift 5.4.2 (2021/6/21)")
#elseif swift(>=5.4)
print("Swift 5.4 (2021/4/26)")
#elseif swift(>=5.3.2)
print("Swift 5.3.2 (2021/1/26)")
#elseif swift(>=5.3.2)
print("Swift 5.3.2 (2020/12/14)")
#elseif swift(>=5.3.1)
print("Swift 5.3.1 (2020/11/12)")
#elseif swift(>=5.3)
print("Swift 5.3 (2020/9/16)")
#elseif swift(>=5.2.4)
print("Swift 5.2.4 (2020/5/20)")
#elseif swift(>=5.2.2)
print("Swift 5.2.2 (2020/4/15)")
#elseif swift(>=5.2)
print("Swift 5.2 (2020/3/24)")
#elseif swift(>=5.1.3)
print("Swift 5.1.3 (2019/12/13)")
#elseif swift(>=5.1.2)
print("Swift 5.1.2 (2019/11/7)")
#elseif swift(>=5.1)
print("Swift 5.1 (2019/9/20)")
#elseif swift(>=5.0.1)
print("Swift 5.0.1 (2019/4/18)")
#elseif swift(>=5.0)
print("Swift 5.0 (2019/3/25)")
#elseif swift(>=4.2.1)
print("Swift 4.2.1 (2018/10/30)")
#elseif swift(>=4.2)
print("Swift 4.2 (2018/9/17)")
#elseif swift(>=4.1.2)
print("Swift 4.1.2 (2018/5/31)")
#elseif swift(>=4.1)
print("Swift 4.1 (2018/3/29)")
#elseif swift(>=4.0.3)
print("Swift 4.0.3 (2017/12/5)")
#elseif swift(>=4.0.2)
print("Swift 4.0.2 (2017/11/1)")
#elseif swift(>=4.0)
print("Swift 4.0 (2017/9/19)")
#elseif swift(>=3.1)
print("Swift 3.1 (2017/3/27)")
#elseif swift(>=3.0.2)
print("Swift 3.0.2 (2016/12/13)")
#elseif swift(>=3.0.1)
print("Swift 3.0.1 (2016/10/28)")
#elseif swift(>=3.0)
print("Swift 3.0 (2016/9/13)")
#elseif swift(>=2.2)
print("Swift 2.2 (2016/3/21)")
#elseif swift(>=2.1)
print("Swift 2.1 (2015/10/20)")
#elseif swift(>=2.0)
print("Swift 2.0 (2015/9/21)")
#elseif swift(>=1.2)
print("Swift 1.2 (2015/4/8)")
#elseif swift(>=1.1)
print("Swift 1.1 (2014/10/22)")
#elseif swift(>=1.0)
print("Swift 1.0 (2014/9/9)")
#else
print("Swift unknown version")
#endif
```



### ・ Swift Version

https://swiftversion.net