How can I programmatically find Swift's version?

前端 未结 6 1521
轻奢々
轻奢々 2020-12-01 10:16

I know I can find the version of Swift I\'m running right now reverting to a Terminal and typing:

xcrun swift --version
Swift version 1.1 (swift-600.0.57.4)
         


        
6条回答
  •  日久生厌
    2020-12-01 10:37

    You can use conditional compilation directives to test for the specific Swift version used to build your project:

    #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
    

提交回复
热议问题