NEW: Master Swift design patterns with my latest book! >>

< Previous: Capturing values   Next: Creating your own structs >

Closures summary

You’ve made it to the end of the sixth part of this series, so let’s summarize:

  1. You can assign closures to variables, then call them later on.
  2. Closures can accept parameters and return values, like regular functions.
  3. You can pass closures into functions as parameters, and those closures can have parameters of their own and a return value.
  4. If the last parameter to your function is a closure, you can use trailing closure syntax.
  5. Swift automatically provides shorthand parameter names like $0 and $1, but not everyone uses them.
  6. If you use external values inside your closures, they will be captured so the closure can refer to them later.

Help support Hacking with Swift

This site is funded by Hacking with Swift supporters who buy my e-books. If you can, please support my work – it comes packed with bonus material!

< Previous: Capturing values   Next: Creating your own structs >
Click here to visit the Hacking with Swift store >>