[Swift 3.0] 標準ライブラリがAPIガイドラインに適用された話

2016.09.14

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Swift3.0の変更点である、
Apply API Guidelines to the Standard Library(標準ライブラリをAPIガイドラインに適用したよ)
の話です。

個人的な主観ですが、Swift2.xまではどちらかというとObjective-Cの影響を濃く残したネーミングが数多く見受けられましたが、 Swift3から、よりシンプルで最適化された名称になりました。

APIデザインガイドラインは下記から確認することが出来ます。
https://swift.org/documentation/api-design-guidelines/

どういった変更?

例を見た方が早いので幾つか挙げてみます。?

UIColor

Swift2.x(今までの例)
let color = UIColor.redColor()
color.colorWithAlphaComponent(0.5)
Swift3.0
let color = UIColor.red
color.withAlphaComponent(0.5)

デフォルトの色指定がシンプルになってます。
また、colorWithAlphaComponentだったのが、withAlphaComponent となっており、意味が重複?していた部分がキレイになりました。

配列

Swift2.x(今までの例)
var list = [1,2,3]
list.insert(5, atIndex: 0)
list.removeAtIndex(0)
list.indexOf(1)
list.minElement()
list.maxElement()
Swift3.0
var list = [1,2,3]
list.insert(5, at: 0)
list.remove(at: 0)
list.index(of: 1)
list.min()
list.max()

insertは第2引数のラベルが変更になっています。
全体的にすっきりとなった印象です。

UIFont

Swift2.x(今までの例)
UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
UIFont.systemFontOfSize(12.0)
UIFont.boldSystemFontOfSize(12.0)
UIFont.italicSystemFontOfSize(12.0)
Swift3.0
UIFont.preferredFont(forTextStyle: .headline)
UIFont.systemFont(ofSize: 12.0)
UIFont.boldSystemFont(ofSize: 12.0)
UIFont.italicSystemFont(ofSize: 12.0)

最初の引数にラベルがついた影響かメソッド名がシンプルになりました。 (長さはあまり変わってないですが。。。)

String

Swift2.x(今までの例)
let word = "Hello"
word.stringByAppendingString("!!")
word.stringByReplacingOccurrencesOfString("ll", withString: "re we g")
word.componentsSeparatedByString("e")
Swift3.0
let word = "Hello"
word.appending("!!")
word.replacingOccurrences(of:"ll", with: "re we g")
word.components(separatedBy: "e"

Stringが全体的に省略されてシンプルになりました。

UIKit

Swift2.x(今までの例)
presentViewController(viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)?)
dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?)
prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)

// TableView
dequeueReusableCellWithIdentifier(identifier: String) -> UITableViewCell?
numberOfSectionsInTableView(tableView: UITableView) -> Int
Swift3.0
present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (@escaping () -> Swift.Void)? = nil)
dismiss(animated flag: Bool, completion: (@escaping () -> Swift.Void)? = nil)
prepare(for segue: UIStoryboardSegue, sender: Any?)

// TableView
dequeueReusableCell(withIdentifier identifier: String) -> UITableViewCell?
numberOfSections(in tableView: UITableView) -> Int

こちらもメソッド名がシンプルになってます。

さいごに

[Swift 3.0] 関数ラベルのルールが変更になった話

でも取り上げたとおり、最初の引数にも名前を付けるようになったことが影響している部分もありますが、全体的に良い感じに短い名前になっていると思われます。

個人的には、 dismissViewControllerAnimated が dismiss になって、completionにデフォルト引数(nil)が入って省略出来るようになったのが嬉しいです。

dismissViewControllerAnimated(true, completion: nil)
↓
dismiss(animated: true)
余談

Swift2系とSwift3.0で同じソースを使う場合は、

#if swift(>=3.0)
  // ここにSwift3.0用のコードを書く
#else
  // ここにSwift2.x用のコードを書く
#endif

のように切り分けることが出来ます。あまり発生しないケースだと思いますが。。。