如何将我的swift 1.2项目迁移到2.0? [英] How to migrate my swift 1.2 project into 2.0?

查看:89
本文介绍了如何将我的swift 1.2项目迁移到2.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在swift推出时开发了一个项目,但最近Apple推出了带有xCode 7.0的swift 2.0新版本。那么如何将我的项目从swift 1.2迁移到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?

推荐答案

在新的Xcode 7 beta中,转到编辑菜单 - >转换 - >到最新的Swift语法

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

这将为您运行代码转换器并显示它将要进行的更改。这些都是自动更改(比如将println更改为print等)。

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).

然后重构代码以使其更像Swift,这里有一些提示:

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).

在适当情况下使用保护声明。通常使用它来减少缩进和嵌套if语句。如果使用得当,这些都非常好。

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.

当转换为/从类型转换(例如String - > NSData,反之亦然)时,使用可用的初始值设定项作为要转换的类型而不是拥有该类型的属性。因此,不要执行 someString.dataUsingEncoding(NSUTF8StringEncoding)执行类似 NSData(someString,encoding:NSUTF8StringEncoding)的操作。请注意,这不是API的实现方式,但我用它作为一个例子来说明事情如何更加Swifty。

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.

这篇关于如何将我的swift 1.2项目迁移到2.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆