2

How do I create a swift package that supports multiple swift versions?

My code compiles fine with swift 4.0, 4.2 and 5.0. If I update swift-tools-version inside Package.swift then I can't use it on 4.0 or 4.2, but if I add 4.2 or 5.0 to swiftLanguageVersions it doesn't recognize the version enums.

If swift-tools-version is set to 4.0, .v5 is not recognized inside swiftLanguageVersions

If swift-tools-version is updated to 5.0 then the package will not work on 4.0

What is the correct way of doing this? Or once you update to the latest version of swift the package stops supporting older versions?

2 Answers 2

2

You can either create separate Package@swift-#.swift files for each version or starting in Swift 4.2 you can use the .version("#") enum.

See: https://forums.swift.org/t/swiftlanguageversions-best-practices-for-libraries/18443

Sign up to request clarification or add additional context in comments.

Comments

1

My approach is to keep swift-tools-version set to the earliest version of Swift that you support, and to simply ignore the swiftLanguageVersions property. It' worked for me so far.

You could also try using conditional compilation to have different versions of your Package in your Package.swift file.

1 Comment

I just tried it, and my conclusion is that I was completely wrong. Note to self: Don't ever, EVER, do that. It needs to compile the file in order to determine which version of Swift to compile the file with ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.