最終更新:2022-02-18 (金) 17:48:06 (803d)  

Swift/Trailing Closures
Top / Swift / Trailing Closures

If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead.

https://docs.swift.org/swift-book/LanguageGuide/Closures.html

func someFunctionThatTakesAClosure(closure: () -> Void) {
    // function body goes here
}

// Here's how you call this function without using a trailing closure:

someFunctionThatTakesAClosure(closure: {
    // closure's body goes here
})

// Here's how you call this function with a trailing closure instead:

someFunctionThatTakesAClosure() {
    // trailing closure's body goes here
}
  • When you use the trailing closure syntax, you don’t write the argument label for the first closure as part of the function call.

メモ

関連