Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate my swift 1.2 project into 2.0?

Tags:

xcode

ios

swift

I have a project developed when swift was introduced but recently Apple has new version of swift 2.0 with xCode 7.0. So how can i migrate my project from swift 1.2 to 2.0?

like image 805
Mehul Chuahan Avatar asked Jul 24 '15 06:07

Mehul Chuahan


People also ask

How do I change the project version in Swift?

Getting New Swift ToolchainGo to Xcode->Preferences->Components section. Now you will see another tab for the “Toolchains” where we can pick the desired Swift toolchain for our local development without changing the Xcode version. Now that, we have successfully downloaded Swift 3.1 development snapshot.


1 Answers

In the new Xcode 7 beta go to the Edit menu -> Convert -> To Latest Swift Syntax

This will run the code converter for you and show you the changes it is going to make. These are automatic changes (like changing println to print and so on).

Then to refactor the code to make it more Swift-like here are some tips:

  • Ensure you are using the new error handling functionality wherever possible (the code conversion tool does this for the most part but sometimes it gets it wrong).

  • Use guard statements where appropriate. In general use it to reduce indentation and nested if statements. These are really nice when used properly.

  • Almost all your global functions can be refactored into protocol extensions. Move generic functions to extensions.

  • When converting to/from a type (for instance String -> NSData and vice versa) use failable initializers with the parameter as the type to convert from instead of having properties on the type. So instead of doing someString.dataUsingEncoding(NSUTF8StringEncoding) do something like NSData(someString, encoding: NSUTF8StringEncoding). Note this is not how the API is implemented but I used it as an example to show how things can be more "Swifty".

  • Use availability checking where useful.

  • Move clean up code to defer blocks as much as possible. This can help redundant duplicated clean up code like file closing, etc.
like image 142
Manav Gabhawala Avatar answered Oct 17 '22 09:10

Manav Gabhawala