LoginSignup
50
49

More than 5 years have passed since last update.

Swift 2.2のプロジェクトをSwift 3.0に書き換えるタスクリスト

Last updated at Posted at 2016-06-22

以下の記事は、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)
50
49
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
50
49