2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Shorthand Argument Names

Last updated at Posted at 2014-06-08

SwiftではClosureを作るときに、束縛変数のエイリアスが組み込まれている。
Scalaではプレースホルダーをつかうやつ。

Closureを一番丁寧に書くとこう

let reversed = sort(names, { (s1: String, s2: String) -> Bool in return s1 < s2}

でも型推論にまかせて型は書かなくてもいいとして、変数自体も別に名前を付けたくない場合がある。
どうせスコープ小さいし。そういう場合には$0とかを使う。シェル変数みたい。

let reversed = sort(names, { $0 > $1 } )

これは一番目の変数が二番目の変数よりも後ろに来ますよということを表すClosure。Scalaだと_なので各変数は一度しか使えないけど、Swiftでは区別されているので何度も使えるみたい。

参照:The Swift Programming language

2
2
0

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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?