如何向Swift 3.0进行数据迁移升级?Migrating Advice, tips,and warnings

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
查看查看588 回复回复4 收藏收藏 分享淘帖 转播转播 分享分享 微信
查看: 588|回复: 4
收起左侧

如何向Swift 3.0进行数据迁移升级?Migrating Advice, tips,and warnings

[复制链接]
swifter 发表于 2016-8-4 20:19:52 | 显示全部楼层 |阅读模式
快来登录
获取优质的苹果资讯内容
收藏热门的iOS等技术干货
拷贝下载Swift Demo源代码
订阅梳理好了的知识点专辑

知道了《Swift 3.0 都改变了什么语法?》,也了解了《是时候适配 Swift 3 了吗?》,那么,有人填过这个坑吗?

好!填坑经验来了!本文作者分享了他从 Swift 2.2 代码迁移到 Swift 3 的经验,以及遇到的坑。这些经验对于全球最大的Swift开发者社区敏捷大拇指Swifthumb.com)上使用新的、酷炫的非 Swift 语言的人也是值得借鉴的。

如何向Swift 3.0进行数据迁移升级?Migrating Advice, tips,and warnings 0

如何向Swift 3.0进行数据迁移升级?Migrating Advice, tips,and warnings - 敏捷大拇指 - 如何向Swift 3.0进行数据迁移升级?Migrating Advice, tips,and warnings 0


上周我花了好几天的时间,将我的Swift代码迁移到了Swift 3.0上面——我不仅迁移了我的开源项目,还包括我的私人项目。总体来看,我觉得这次迁移的体验还算“OK”。在这个过程中,我遇到的最大的问题,就是Xcode的迁移工具一次性的显示了太多的变更和错误,这让我一时间有些不知所措。但是在我看来,最有意思的事情,就是带上耳机,处理代码。

I spent most of my free time last weekend and a few days of last week on migrating my Swift code to Swift 3.0 — I migrated my open source projects as well as my private side projects. Overall, I would say my experience was “OK”. It definitely could have been better, but I think the largest problem was overcoming the cognitive hurdle of seeing all the changes and errors from Xcode’s migration tool at once. The best thing to do is wipe away the tears, put your headphones on, and start hacking.

如何向Swift 3.0进行数据迁移?Migrating Advice, tips, and warnings 3 eidhof-programmer

如何向Swift 3.0进行数据迁移升级?Migrating Advice, tips,and warnings - 敏捷大拇指 - 如何向Swift 3.0进行数据迁移?Migrating Advice, tips, and warnings 3 eidhof-programmer

真正的Swift程序员,敢于直面迁移升级!
A real-life Swift programmer, ready for migration. Photo taken just before migrating to Swift 3.0.





1、开源迁移策略 Open source migration strategy

当我在迁移开源库的时候,我基本的工作流程是这样的:

Here’s my basic git workflow for migrating my open source libraries:

  • 所有的一般性开发都是在develop分支上完成的;
  • 在develop上创建一个Swift2.3分支,然后运行Swift 2.3版本的Xcode分支工具;
  • Swift2.3分支上创建一个Swift3.0分支,然后运行Swift 3.0的迁移工具。


  • All normal development happens on the develop branch
  • Create a swift2.3 branch from develop and run Xcode’s migrator for Swift 2.3
  • Create a swift3.0 branch from the swift2.3 branch and run Xcode’s migrator for Swift 3.0


于是下面的这个模型就形成了:

This creates the following model:

[Swift] 纯文本查看 复制代码
o-----o develop (swift 2.2)
       \
        o-----o swift2.3
               \
                o-----o swift3.0


如何向Swift 3.0进行数据迁移? 1

如何向Swift 3.0进行数据迁移升级?Migrating Advice, tips,and warnings - 敏捷大拇指 - 如何向Swift 3.0进行数据迁移? 1


到这一步,我的计划是让这些分支保持同步状态。也就是说,Swift2.3会在develop之前,Swift3.0要在swift2.3之前。我的最终目标是,在Xcode 8发布之后,我要在每一个分支中合并更改,然后将它们放在develop上的单一commit中。

My plan for now is to keep these branches in sync like what you see above. That is, swift2.3 will be ahead of develop and swift3.0 will be ahead of swift2.3. The end goal will be to merge changes from each branch back into a single squashed commit on develop when the final release of Xcode 8 is out.

[Swift] 纯文本查看 复制代码
                           merge 2.3           merge 3.0
o------o                    o                    o-----------o develop
        \                  /                    /
         o--- swift2.3 ---o                    /
                           \                  /
                            o--- swift3.0 ---o


如何向Swift 3.0进行数据迁移? 2

如何向Swift 3.0进行数据迁移升级?Migrating Advice, tips,and warnings - 敏捷大拇指 - 如何向Swift 3.0进行数据迁移? 2


develop中的每一次合并,都将会库的一次主要release。例如,如果库当前为v2.0,那么Swift 2.3合并之后,库将会升级为v3.0,Swift 3.0合并之后,库会升级为v4.0。这样做能让客户安全的在个版本之中进行合并,确保了semantic versioning

Each merge into develop will be a major release of the library. For example, if the library is currently at v2.0, then the Swift 2.3 merge will result in v3.0 of the library and the Swift 3.0 merge will result in v4.0 of the library. This ensures semantic versioning and allows clients to safely migrate between versions at their own pace as they adopt the next version of Swift.




2、私人项目 Private projects

处理私人项目的时候,我直接向Swift 3.0进行了迁移。我使用了CocaPads,因此我要先迁移所有依赖。

For my private projects, I migrated directly to Swift 3.0. I use CocoaPods, so I migrated my dependencies first. This means following the steps above for each open source library (or private pod).

现在,唯一的依赖就是我自己的库,由于我自己对所有代码都有控制权,因此这个过程比较轻松。然而,如果你有第三方依赖,那么我建议你和当前的维护者进行沟通,商讨迁移的计划。我觉得大多数项目都会有第三方依赖。例如,AlamoFire有Swift2.3和Swift3.0分支。如果需要的话,你也可以自己对第三方库进行迁移,然后给维护者发送一个pull request,直到他给出解决方案。然而,在提交迁移pull request之前,你一定要和维护者先取得联系。

So far, the only dependencies I have for this project are my own libraries, which made this quite easy since I control all of the code. However, if you have third-party dependencies, then I would recommend opening an issue on the project to discuss migration plans with the current maintainers. I expect most popular projects are doing something similar to what I described above. For example, AlamoFire has a swift2.3 and swift3.0 branch. If needed, you can fork and migrate third-party libraries yourself — then submit a pull request or use your fork until the maintainers offer a solution. However, you should definitely reach out to project maintainers before submitting a pull request for migration.

在Xcode 8正式发布之前,你将会需要将你的pod指向这些新的分支:

Until Xcode 8 is final, you’ll need to point your pods to these new branches:

[Bash shell] 纯文本查看 复制代码
 pod 'MyLibrary', :git =>'https://github.com/username/MyLibrary.git', :branch => 'swift3.0'


这会告诉CocoaPods要从swift3.0分支上获取最新数据,而不是最新的已发布版本上。

This tells CocoaPods to fetch the latest on the swift3.0 branch, instead of the latest published version.

在你的依赖和Podfile完成上传之后,你可以运行pod update将Swift 3.0添加到每一个库中。之后你就可以迁移主应用了。我建议你用一条commit来进行这个迁移——上传所有依赖,迁移应用,然后commit——这样能让操作历史非常简洁。

Once your dependencies and Podfile are updated, you can run pod update to bring in the Swift 3.0 versions of each library. Then you can migrate your main app. I suggest commiting all of this migration in a single commit — update all dependencies, migrate your app, then commit — to keep your history clean.




3、一些Tips

These things probably go without saying, but it doesn’t hurt to reiterate.

  • 在迁移代码之前,你要对代码进行一次详尽的测试。迁移过程中会出现大量的变更,要想确保你没有破坏任何代码,最好的方式就是进行测试。
  • 在迁移的过程中不要进行任何重构。
  • 如果你还没有迁移,那就先等等下一个beta。我就犯了这样一个错误,我迁移的时候使用的是beta2,但是在我完成迁移之后的一天,beta3就推出了。虽然两个beta之间的变更并不大,但是还是需要做一些工作的。
  • 不要想着一天之内就完成所有迁移。这是一个需要花费很多天的工作。我一天之内只迁移了1-2个库,之后又花了好几天的时间迁移完成的应用。
  • 当你不知道如何迁移某段代码的时候,你可以尝试在这个页面找到对应的方案


  • Make sure you have decent test coverage on the code you are migrating. The changes are massive and having a list of green checkmarks once the migration is over is the best way to ensure you haven’t broken anything.
  • Resist doing any other refactoring during the migration. Migrate, commit, then make Swifty API changes in a follow-up commit to bring your code up-to-date with the latest API guidelines.
  • If you haven’t migrated yet, wait until right after the next beta. I made the mistake of migrating during beta 2 after it had been out for two weeks. The next day, beta 3 was released.  The changes between the betas weren’t as large as the initial migration, but they weren’t trivial either — and the migrator doesn't work between betas, so you have to apply each Xcode fix-it individually. Update: the migrator does work between betas, but you have to run it manually. (I thought I tried this, but I might be mistaken.) I would rather have waited the extra day to migrate all at once with beta 3, then tackle the changes in beta 4 two weeks later.
  • Do not migrate everything in a single day. Accept that this is a multi-day task. I did one or two libraries in a day and then spent a few days migrating the full app. Commit or git stash a work-in-progress and resume the next day.
  • When in doubt about how to properly migrate a piece of code, consult the new swift-evolution proposal status page to find the corresponding proposal. In the Implemented for Swift 3 section, you’ll find all of the proposals that have been implemented for Swift 3. Note that not all of these may be in the current beta yet.





4、Bugs

Xcode的迁移工具还不完美。有的时候测试目标的迁移会失败,或是只能部分迁移应用和框架目标。当发生这样的事情的时候,你可以尝试重新运行工具,但是最好的方法还是手动做出变更。我在迁移的过程中遇到了下面这些问题:

Xcode’s migration tool is not perfect.  It would sometimes fail to migrate test targets, or only partially migrate app and framework targets. When this happens, you can attempt to run the tool again, but it’s probably best to make changes manually. Here are some of the specific issues that I saw:
  • XCTAssert*()内部的一些expressionns没有被迁移;
  • 闭包内的一些closures没有被迁移;
  • 一些waitForExpectations(timeout: )没有被迁移;
  • 在一些特定的情境下,将NSIndexPath迁移至IndexPath,经常会导致一些让人头疼的事情,例如 (indexPath as! NSIndexPath).section 。
  • 带有NSDate值的Enums会被迁移至case myCase(Foundation.Date),而不是case myCase(Date)。
  • 有的适合optional协议方法不会迁移,这回导致很难解决的bug。


  • Some expressions inside of XCTAssert*() did not migrate.
  • Some expressions inside closures did not migrate.
  • Sometimes waitForExpectations(timeout: ) did not migrate.
  • Migrating NSIndexPath to IndexPath when used in certain contexts often resulted in derpy things like (indexPath as! NSIndexPath).section.
  • Enums with associated NSDate values migrated to case myCase(Foundation.Date) instead of case myCase(Date).
  • Sometimes optional protocol methods did not migrate, which can produce hard-to-find bugs.





5、总结 Conclusion

迁移Swift代码总体来说是一件比较无聊的事情。当你第一次看到那么多变更和错误提示的时候,你会产生认知负荷。这些信息一次性的向你扑来,你会手足无措。但是无论如何你都要迁移。当你完成之后,你会感觉很爽。

Migrating Swift code is as fun as fixing your CI servers for the Nth time and as exciting as waiting for an hours-long test suite to run on your local machine. Even waiting for your code to compile is probably more fun than migrating to Swift 3.  When you first see all the changes and errors that the migrator produces, it will be cognitive overload. It’s a lot to absorb at once. You can always find comfort in git reset --HARD, but you will have to migrate eventually. And when you finish, you’ll feel great.

这个周末我花了大量的时间写Swift 3.0代码——而不是迁移它们——感觉不错。

This weekend I got to spend my time writing Swift 3.0 code — not migrating it — and it was awesome.


更多Swift3.0资料,可以到全球最大的Swift开发者社区敏捷大拇指Swifthumb.com)查看这个淘帖专辑《Swift 3》,不谢~




原文:Migrating to Swift 3: Advice, tips, and warnings
作者:Jesse Squires

都看到这里了,就把这篇资料推荐给您的好朋友吧,让他们也感受一下。

回帖是一种美德,也是对楼主发帖的尊重和支持。

*声明:敏捷大拇指是全球最大的Swift开发者社区、苹果粉丝家园、智能移动门户,所载内容仅限于传递更多最新信息,并不意味赞同其观点或证实其描述;内容仅供参考,并非绝对正确的建议。本站不对上述信息的真实性、合法性、完整性做出保证;转载请注明来源并加上本站链接,敏捷大拇指将保留所有法律权益。如有疑问或建议,邮件至marketing@swifthumb.com

*联系:微信公众平台:“swifthumb” / 腾讯微博:@swifthumb / 新浪微博:@swifthumb / 官方QQ一群:343549891(满) / 官方QQ二群:245285613 ,需要报上用户名才会被同意进群,请先注册敏捷大拇指

嗯,不错!期待更多好内容,支持一把:
支持敏捷大拇指,用支付宝支付10.24元 支持敏捷大拇指,用微信支付10.24元

评分

参与人数 1金钱 +10 贡献 +10 专家分 +10 收起 理由
Anewczs + 10 + 10 + 10 32个赞!专家给力!

查看全部评分

本帖被以下淘专辑推荐:

i0n1c 发表于 2016-8-4 22:57:11 | 显示全部楼层
真正的Swift程序员,敢于直面迁移升级!哈哈!填坑~~~~~~
买定离手 发表于 2016-8-7 01:15:00 | 显示全部楼层
真正的Swift程序员,敢于直面迁移升级!
A real-life Swift programmer, ready for migration.

哈哈
3rdev 发表于 2016-9-3 20:04:34 | 显示全部楼层
今天来好好玩玩Swift 3~~~
移动 发表于 2016-10-30 07:45:31 | 显示全部楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

做任务,领红包。
我要发帖

分享扩散

都看到这里了,就把这资料推荐给您的好朋友吧,让他们也感受一下。
您的每一位朋友访问此永久链接后,您都将获得相应的金钱积分奖励
热门推荐

合作伙伴

Swift小苹果

  • 北京治世天下科技有限公司
  • ©2014-2016 敏捷大拇指
  • 京ICP备14029482号
  • Powered by Discuz! X3.1 Licensed
  • swifthumb Wechat Code
  •   
快速回复 返回顶部 返回列表