NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

Summary: Closures

Paul Hudson    @twostraws   

Updated for Xcode 13.3

We’ve covered a lot about closures in the previous chapters, so let’s recap:

  • You can copy functions in Swift, and they work the same as the original except they lose their external parameter names.
  • All functions have types, just like other data types. This includes the parameters they receive along with their return type, which might be Void – also known as “nothing”.
  • You can create closures directly by assigning to a constant or variable.
  • Closures that accept parameters or return a value must declare this inside their braces, followed by the keyword in.
  • Functions are able to accept other functions as parameters. They must declare up front exactly what data those functions must use, and Swift will ensure the rules are followed.
  • In this situation, instead of passing a dedicated function you can also pass a closure – you can make one directly. Swift allows both approaches to work.
  • When passing a closure as a function parameter, you don’t need to explicitly write out the types inside your closure if Swift can figure it out automatically. The same is true for the return value – if Swift can figure it out, you don’t need to specify it.
  • If one or more of a function’s final parameters are functions, you can use trailing closure syntax.
  • You can also use shorthand parameter names such as $0 and $1, but I would recommend doing that only under some conditions.
  • You can make your own functions that accept functions as parameters, although in practice it’s much more important to know how to use them than how to create them.

Of all the various parts of the Swift language, I’d say closures are the single toughest thing to learn. Not only is the syntax a little hard on your eyes at first, but the very concept of passing a function into a function takes a little time to sink in.

So, if you’ve read through these chapters and feel like your head is about to explode, that’s great – it means you’re half way to understanding closures!

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.