Migrating a module written in Swift 3.0 to Swift 2.3? - ios

The company I currently work for supports legacy products written in Swift 2.3 (I don't agree with it). At the moment they currently want to continue rolling out features supporting the legacy version and thus, I'm currently in a bind concerning a pod written in Swift 3.0.
I attempted to convert the syntax back to Swift 2.3 by installing Xcode 7.3.1 with the intention of hopefully migrating that code. Unfortunately, since it's a module written in Swift 3.0 my efforts were futile.
I'm out of suggestions though I'm not opposed to writing the code in the module in Swift 2.3 however I know I'll be looking at a tedious process for the long run. What steps should I take to migrate a module written in the most updated version of Swift back to version 2.3?

Firstly, you can use Xcode 8 to do this. Select each of your schemes and then set 'use legacy swift version' in build settings to yes. Compile and then run. Swift will start presenting lots of errors. For example, this code (in swift 3):
SKAction.rotate(byAngle: CGFloat(M_PI_2), duration: 10)
is
SKAction.rotateByAngle: CGFloat(M_PI_2), duration: 10)
in swift 2. You have to manually fix all the errors that swift says, so you need a clear understanding of the old swift syntax.

Related

Unsupported Swift Version 3.x in Xcode 10.3

I keep downloading sample codes and many times it says "Unsupported Swift Version 3.x...Use Xcode 10.1 to migrate the code to Swift 4".
Why is it not possible to migrate to Swift 4 using Xcode 10.3 or Xcode 11 beta?
More importantly, I am using Swift 4.2 in my own Project that has several dependancies as well through Pods. If I don't upgrade to Swift 5.1 or later, it seems my own project would start getting such errors in Xcode 12 or so, correct? While I can convert my own code to Swift 5.1, I have no idea if dependencies such as SwiftyDropbox would migrate to Swift 5.1 so easily. What implications it would have for my project in future and what could I do to future proof my project in that case?
Ask Apple. The company has decided – certainly for good reasons – to migrate files only to the Current Swift Syntax. Feel free to create legacy system partitions with older versions of Xcode to migrate code step by step.
The grace period to update the Swift version is long enough to ensure a smooth transition. At least Xcode allows to use the 3 most recent Swift versions in the same project. However you are responsible to maintain the version control. Check the dependencies periodically for updates. If a dependency is not able to release an update within the grace period it's not much good.

Convert from Swift 3.0 to Swift 2.3

I have a static library that I converter from old Swift to Swift 2.3 and later to Swift 3.0.
Now I need both version (2.3 and 3.0) of the library, but I forgot to take a backup of version 2.3. And I only have 3.0.
Is there a simple way to convert back from Swift 3.0 to Swift 2.3?
Simple, not really, but this will work.
If you don't have it, download XCode 7.3.1 from Apple
Open your library in the 7.3.1 version and correct whatever errors appear
Migrate the corrected code from step 2 into XCode 8.2 or lower (8.3 will not support Swift 2.3)
As mentioned in the comments, maintaining your code in a version control system will spare you problems like this in the future, and is generally just a good idea.
You don't even need to set up your own system, since git is part of MacOS, and XCode can handle simple git features for you.

Is it possible to use libraries written in an old version of swift in a project with a newer version of swift?

I'm working on a project in Swift 3. However, many libraries are still using Swift 2.3.
Is there a way of making use of them in my project as they are?
Since it's possible to use Objective C libraries in Swift, I figure there's a chance.
Maybe this reference from Apple answers the question:
unfortunately it would seem to be impossible:
First, Swift 2.3 and Swift 3 are not binary compatible so your app's entire code base needs to pick one version of Swift.
https://developer.apple.com/swift/blog/?id=36

How can I use swift 2 and swift 3 at the same time?

Although swift 3 is released, I don't want to update my code just yet, because a lot of the CocoaPods I used are still in swift 2. I don't know what the migrator will do to those pods. Will they remain unchanged, causing the project to not build successfully? Or will they be migrated to Swift 3 as well? I don't know!
On the other hand, I really want to try out the new syntax and other new features in Swift 3. They seem really cool!
How can I continue developing my existing projects with Swift 2, but for other projects, compile with Swift 3?
In other words, how can I choose a compiler for a project?
How can I continue developing my existing projects with Swift 2, but for other projects, compile with Swift 3?
Install both Xcode 7 (which has Swift 2) and Xcode 8 (for Swift 3) and use the xcode-select command line tool to switch between them.

Swift 3.0 will not be 2.2 source compatible... will I have to rebuild my apps?

http://thenextweb.com/apple/2016/05/09/swift-3-0-developer-previews-wwdc/#gref
Does this mean anything? Will I have to completely rebuild my apps or merely convert 2.2 syntax to 3.0?
Swift 3.0 will not be compatible with 2.2. Here is the list of the current proposals, it also details what has been implemented already, and what will be :
https://github.com/apple/swift-evolution
You will not have to entirely rebuild your App, but you will have to adapt your code.
I'd say no. Every Swift app comes with its own runtime and standard libraries of whatever Xcode version you used to build it. Some of my Swift 1.0 app still runs on the latest version of El Capitan.
Source incompatibility means you can't take valid code written in Swift 2, feed it to the Swift 3 compiler and expect everything to go smoothly. There will be errors and deprecation warnings, much of which you already see today with Swift 2.3.
You also can't take a compiled application written in Swift 2 and make it run without modification on a Swift 3 runtime either. Swift is lacking a stable Application Binary Interface (ABI) at the moment. That's why every Swift app must carry its own runtime. OS X and iOS don't provide those at the system level. Hence Swift app tends be larger in size, more of a problem on iOS than OS X.
With Swift 3, Apple is trying to establish a stable ABI so your compiled Swift 3 can run without modification on the Swift 4 runtime, whenever that comes.
What about source code compatibility? No one knows what Chris Lattner has in mind or what he considers archaic. Removing the ++ and -- operator in Swift 3 seem superfluous to me. And why kill C-style for loop too?
I'm currently adapting all my code to the present state of Swift 3. The existing code is certainly not compatible; most methods have been renamed, case names have been lowercased, and so on. To what extent Apple will assist by providing a migration tool is unknown, as that's in the future.
#CodeDifferent provided a great answer, but I'd like to extend on it.
AFAIK, if your Swift 2 app is compiled with its own runtime linked in (i.e., not relying on the system runtime), then your app will run fine & there's no need to rebuild on a Swift 3 machine to run on a Swift 3 machine. But if there's no runtime compiled into the app, then your app won't run on a machine with a Swift 3 ABI.
Of course, if you are going to compile with application with Swift 3, then you will have to re-build.

Resources