Swift 5.3(SE-0279、SE-0286)から追加された Multiple Trailing Closures について。
例えば、UIView.animate メソッドのような引数に複数の Closure を持つ場合の Trailing Closure は Swift5.2 までは次のように書いていた。
UIView.animate(withDuration: 0.3, delay: 0, options: [], animations: { // ... }) { _ in // ... }
この従来の書き方は現行のバージョンでもコンパイルエラーにはならず使える。
Swift 5.3 からの新しいルールでは Trailing Closure を次のように書ける。
UIView.animate(withDuration: 0.3, delay: 0, options: []) { // ... } completion: { _ in // ... }
最初の Closure は引数ラベルを省略し、2つ目以降の Closure には引数ラベルを付ける。
ちなみに最初の Closure のラベルを省略せず書くとコンパイルエラーになる。
// compile error UIView.animate(withDuration: 0.3, delay: 0, options: []) animations: { // ... } completion: { _ in // ... }