Migrating to Swift 3

What's Swift 2.3

Swift 2.3 is a minor update from Swift 2.2.1. The primary difference between Swift 2.2.1 and Swift 2.3 is that it is intended to be paired with Apple’s macOS 10.12, iOS 10, watchOS 3, and tvOS 10 SDKs. It also updates the underlying LLVM and Clang versions to match with those in the Swift 3 compiler.

In a word, it just like this: Swift 2.3 = Swift 2.2.1 + SDKs updated (for new devices OS version)

Xcode 8.2 Beta Release Notes:

Xcode 8.2 is the last release that will support Swift 2.3.

日本語:

久々にSwift公式ブログが更新されました。Swift 2.3がリリースされるようです。タイトルの通りSwift 2.3はSwift 2.2.1のマイナーアップデートバージョンという位置づけのようで、今後アップデートされるOS向けのSwiftみたいですね。Swift 3.0に「まだ」移行したくない人向けのリリースのようで、Xcode8に実装されるようです。

Swift 2.3 は Swift 2.2 からのマイナーアップデートで, Swift 2.2 から比較的低いコストで移行することができます. Swift 3 は Swift 2.2 から大きく改善されていますが, 移行にあたっては大きな変更が必要になります.

What's Swift 3.0

Swift 3.0 is a major language release. It is not source-compatible with Swift 2.2 and 2.3.

Swift 3 is a source-breaking release, largely due to the changes in SE-0005 and SE-0006. These changes not only impact the names of the Standard Library APIs, but also completely change how Objective-C APIs (particularly from Cocoa) import into Swift. Many of the changes are largely mechanical, but they can be numerous in a typical Swift project.

Ted Kremenek:

The rest of the unimplemented proposals do not make Swift 3. This leaves us with the question of what to do with them. These proposals represent the known and reviewed changes we want to make to Swift, but inevitably there will also be changes that we don’t even know about today that we will want to take into Swift that can impact core source stability. That said, we also have a very strong desire to maintain source compatibility with Swift 3 and Swift 4 as much as possible to provide some stability for which Swift users to build upon. The challenge of course is reconciling these diametrically opposing goals: maintaining source stability while having the ability to incorporate more core (and important) language changes that are possibly source-breaking.

The Swift team at Apple has reflected on this and decided what it “means” for Swift 3 to be source compatible with Swift 4 and later releases going forward. Our goal is to allow app developers to combine a mix of Swift modules (e.g., SwiftPM packages), where each module is known to compile with a specific version of the language (module A works with Swift 3, module B works with Swift 3.1, etc.), then combine those modules into a single binary. The key feature is that a module can be migrated from Swift 3 to 3.1 to 4 (and beyond) independently of its dependencies.

see: End of source-breaking changes for Swift 3 - Ted Kremenek

AppleのSwift開発チームに所属するTed Kremenek氏は9月13日、Swiftのメジャーバージョンアップ版「Swift 3.0」のリリースを発表しました。

オープンソース化後、はじめてのメジャーバージョンアップで、言語のコアおよび標準ライブラリに多数の改良が行われています。

Swift 3にはソースコードの互換性失われる変更が含まれていて、主たる原因はSE-0005およびSE-0006によるものであるとのこと。これらの変更は標準ライブラリのAPIを呼び出す際の名前に影響があるだけでなく、Objective-C APIをSwiftにインポートする際の仕組みにも影響があるとされています。大部分は機械的に対処できるそうですが、典型的なSwiftプロジェクトには影響する部分が多数存在するようです。

5月半ばに Chris Lattner は、“Winding down the Swift 3 release” と題したメールをコミュニティに送った。そこには ABI(Application Binary Interface) の安定化と、そのために必要な generics の機能を完成を、Swift 3.0 のリリースから見送ることが書かれている。また Swift 3.0 では API naming という困難に向き合い、ソースコードレベルでの安定化が図られたということが強調されている。つまり将来のバージョンの Swift でも、可能な限りソースコードの互換性が維持されることが期待されている。

参照:

Swift 2.3 OR Swift 3 ?

Xcode 8.2 Beta Release Notes:

Xcode 8.2 is the last release that will support Swift 2.3.

It seems that is no meaning to migrate to Swift 2.3.
And there is no much reference about how to migrate to Swift 2.3.
Anyway you would still need to migrate to Swift 3.0 from Swift 2.3 since Xcode 8.3 or later.

So what would be the problems while migrating to Swift 3 ?

Environment requirement

For development on Apple’s platforms, Swift 3.0 ships as part of Xcode 8.0.
And Xcode 8.1 is available now and OS X 10.11.5 or later is requirement.
Also Xcode 8.0 beta requires a Mac running macOS 10.11.4 or later.
iOS 10 SDK を用いた開発には, Xcode 8 が必要になります。

Library support requirement

Since Swift 2.2 is not supported from Xcode 8, which means that libraries be written in Swift 2.2 will make compiling errors.
It is necessary to wait for the libraries update to support Swift 3, or update it manually.

サードパーティのライブラリを利用しているのであれば, それらのライブラリが iOS 10・Swift 2.3 / 3 に対応しているかを事前に確認しておきましょう. メジャーなライブラリでは Swift 2.3 と Swift 3 のコードが別々のブランチでメンテナンスされているようですが, ライブラリによっては Swift 3 のみの対応となっているものもあり, 注意が必要です.

Source change requirement

Since Swift 3 is a source-breaking release, it is not source-compatible with Swift 2.2 and 2.3.

Swift 2.2 から Swift 2.3 / 3 への移行には Xcode 8.0 の migration assistant を利用することが出来ます. これだけで完全に移行することは出来ませんが, まずは試してみる価値のある機能だと思います. 当然ですが移行の前後で VCS 等できちんとコミットしておき, 修正された箇所がわかるようにすることをおすすめします.

Swift 3 では API のデザインガイドラインが定められています. 標準ライブラリの API にも多くの変更が加えられているため対応が必要です.

既存のコードの API も新しいガイドラインに沿ったものにするのが望ましいですが, これも変更に大きなコストがかかるので, 一旦 Swift 3 でアプリをビルドできるようになってから対応しても遅くないと思います.

What should be noticed during migrating

返り値を利用しない場合の警告

返り値を返す関数を呼び出しているにもかかわらず, その返り値を利用していない場合, Swift 3 のコンパイラは warning を吐きます. Swift 2 では @warn_unused_result をつけることで同様の warning を出すことが出来ました.

この warning を抑制するには, 返り値を _ に代入するか, 関数に @discardableResult をつけます. コードによってはこの warning が大量に出てしまうので, うまく抑制した方が他の警告に集中しやすくなります.

Data から String への変換

プッシュ通知のトークンは NSData 型で与えられており, これを String 型に変換するには token.description のようにするのが一般的でした.

Swift 3 ではプッシュ通知のトークンは Data 型で与えられ, これを従来の方法で String 型に変換すると 32 bytes という文字列に変換され使い物になりません.

String(format: "%@", token as CVarArg) とすることで, うまく String 型に変換することが出来ます.

CFBundleShortVersionString

これは Swift 3 とは直接関係ないのですが, Swift 3 に対応したライブラリがβ版として配布されている場合に CFBundleShortVersionString1.0.0-beta.1 のような値が設定されていることがあります. このような -beta などが使われたライブラリを含むアプリを iTunes Connect にアップロードしようとすると, エラー になりました. CFBundleShortVersionString1.0.0 のように修正してビルドすることで無事アップロードできました.

see:
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.暗闇より夜魔来たる-1あなたはきっとこんな私をお許しにはならないでしょう…ですが、私はあなたを守る以外の何かを...
    波沙诺瓦阅读 8,522评论 0 7
  • 1.暗闇より夜魔来たる-1あなたはきっとこんな私をお許しにはならないでしょう…ですが、私はあなたを守る以外の何かを...
    波沙诺瓦阅读 5,924评论 1 2
  • 陽の光 闇の月 陽も月も異なれど、同じように地上を照らす。けれど、両者は決してまみえることはない。陽が輝くとき月は...
    波沙诺瓦阅读 6,763评论 0 7
  • 终于结束了!终于结束了!终于结束了! 内心其实是无比开心的,虽然在准备前,相当繁琐,就我们的PPT我们就已经改了不...
    英俊的后花园阅读 1,364评论 0 1
  • 吃一堑长一智应该是个延伸词,而不是用来描述过去。一直以来的理解都有误区,总觉得,一个人做了让我失望的事,以后不理这...
    piupiupiu123阅读 2,784评论 0 0

友情链接更多精彩内容