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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With