Swift 3 is awesome and all, but sometimes you need to remain at Swift 2.3. As you know Xcode 8 also supports Swift 2.3. Great! But what if you want to add a new third party library that is written in Swift 3 to your Swift 2.3 project? How to downgrade from Swift 3 to Swift 2.3 in Xcode 8?
Basically I’ll be taking one of my Swift 2.3 projects and try to add a Swift 3 file to it.
When you open a Swift 2.3 project in Xcode 8 you are greeted with this dropdown.
We want to remain at Swift 2.3 so click on “Later”.
Click “Later” on the second dropdown too.
Go ahead and “Build & Run”. If all goes well than skip this next version, but most probably you will get an error like this one:
What the compiler is basically saying is that it sees some code with old versions of Swift (not latest – Swift 2.3) but the project is not set up to handle this. Either we need to specify that the project should use Swift 2.3 or convert to the latest version of Swift. We will tell the compiler to use the Swift 2.3 version.
Note that this error is currently in a pod, but could be anywhere in your project tree. I’m going to show you how to “allow” Swift 2.3 in this pod. Basically we need to enable “Legacy Swift Language”. Select your pod framework (by clicking on it in the error navigator) and under Build Settings find “Use Legacy Swift Language Version”
Set it to “Yes”. Than “Build & Run” again to test it.
If you take a close look at the error message you will see that we have solved this problem, but another one similar to this one jumped up in the main project. Resolve it in the same fashion as you did this one. Set “Yes” to the “Use Legacy Swift Language Version” in your project Target this time.
What we did here is that we enabled Swift 2.3 code to be used on the Target. This is needed to the next step too when we are going to add a Swift 3 file. For now just “Build & Run” to see if everything compiles fine.
Hurray! It compiles and builds!
Adding a Swift 3 file
Next I’m going to show you how you can add a Swift 3 file to your Swift 2.3 project and than revert that file back to Swift 2.3. Any Swift 3 file will do. Just drag and drop it into your project.
Build your project and you’ll be greeted by at least one error.
In this example we have an error because in Swift 2.3 the UITabBar does not have a member called “isHidden”. It’s actually “hidden” in Swift 2.3. Let’s change it.
Sweet! The error disappears.
But not all errors are as simple as this one. Furthermore, you need to know Swift 2.3 syntax by heart to fix this. Not all of us are that “smart”, so one way you can find the right syntax is to get help from Xcode. Start typing your code and it will help you.
Note that even this requires some minimal knowledge of Swift 2.3 syntax. In this case you need to know / guess the first letter of the function or scroll through a lot of them after hitting the “.”
Luckily there is an even easier way to convert this file. First let’s change the code back to Swift 3 syntax. Than go to Edit > Convert > To Current Swift Syntax…
Remember, this will only work if you have already set the “Yes” to “Use Legacy Swift Language Version” in your Target as shown earlier.
Now select Swift 2.3 and click on “Next”.
Follow the steps. While this might work for smaller projects, it made a mess out of mine, so I’ll just stick with manually fixing the error(s).
As you can see there are no one shot solutions. I found that doing it manually worked best.
And that’s a wrap! Please note that downgrading to Swift 2.3 is not a good idea in the long run. Xcode 8 does support Swift 2.3 and Swift 3 for now but the next release will NOT support Swift 2.3. Better upgrade to Swift 3 soon