以下の記事は、APIを叩いて表示する普通のiOSアプリケーションを想定しています。
Swift 2.2のプロジェクトをSwift 3.0に移行する際のタスクリストです。
Xcode 8にConverterが付いたので、だいたいは自動でやってくれます。
細かい部分はMigration Guideを参照してください。
Swift 2.2 -> Swift 3.0のタスクリスト
- メソッドの呼び出し時にArgumentLabelを付ける
- NSPrefixのクラスをSwift 3.0のFoundationクラスに移行
- NSDate -> Date
- NSURL -> URL
- NSURLRequest -> URLRequest
- NSIndexPath -> IndexPath
- NSNotificationCenter -> NotificationCenter
- NSNotification -> Notification
- NSOperation -> Operation
- NSBundle -> Bundle
- NSCalendar -> Calendar
- NSTimer -> Timer
- GCDをDispatchQueueに移行
- API Guidelineに合わせる
Boolのpropertyにはisを付ける- enumのcaseをlowerCamelCaseにする
※ 追記
「Boolのpropertyにはisを付ける」というのは私の誤解でした。
「Boolのimmutableなメソッドもしくはpropertyはreceiverに対するassertionとして書く」というのが正しいです。
Uses of Boolean methods and properties should read as assertions about the receiver when the use is nonmutating, e.g. x.isEmpty, line1.intersects(line2).
https://swift.org/documentation/api-design-guidelines/
is
を付けるようになったのはObjective-C由来のメソッドとPropertyのみのようです。
https://github.com/apple/swift-evolution/blob/master/proposals/0005-objective-c-name-translation.md
ArgumentLabelの仕様変更
一番対応が大変です。
Swift 2.2
メソッドの呼び出し時、1つ目のパラメータはデフォルトではargument labelが付きません。
2つ目以降のパラメータはデフォルトでパラメータ名がargument labelとして付きます。
func someFunction(firstParameterName: Int, secondParameterName: Int) {
}
someFunction(1, secondParameterName: 2)
Swift 3.0
すべてのパラメータにはデフォルトでパラメータ名がargument labelとして付きます。
func someFunction(firstParameterName: Int, secondParameterName: Int) {
}
someFunction(firstParameterName: 1, secondParameterName: 2)
パラメータ名の前に _
をつけることでargument labelを消すこともできます。
Xcode 8のConverterを使うとこちらに変換されます。特に消したい理由がない限りargument labelは付けるのをおすすめします。
func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
}
someFunction(1, secondParameterName: 2)