はじめに
こんな感じで当たり前のように出てくる$0
はなんなのでしょうか
answers.map { $0.text }
ついさっき理解した(たぶん)ので記事にしときます!
$0の正体
結論
クロージャの省略記法
$0は引数の1つ目
$1は引数の2つ目
•
•
•
例えば?
このようなクロージャがあったとします。
func test(callback: (Int) -> Void) {
callback(1)
}
通常の場合はこのようになります。
test { int in
print(int)
}
これを省略記法を使用して書くとこのようになります。
test {
print($0)
}
かんたん!!
おわり
この記法がよく使われているmap
, filter
, reduce
とかはなんか難しそうで避けてました笑
でも理解したら意外と簡単でびっくり!!
これからは使っていこうと思います!!