Mehul Chuahan
Mehul Chuahan

Reputation: 752

How to migrate my swift 1.2 project into 2.0?

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?

Upvotes: 14

Views: 5131

Answers (1)

Manav Gabhawala
Manav Gabhawala

Reputation: 997

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.

Upvotes: 28

Related Questions