GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

What is a closure?

Swift version: 5.10

Paul Hudson    @twostraws   

If you're here because you find closures hard, that's OK: most people find closures hard. But in truth, closures aren't actually that complicated, so I hope I can explain them to you quickly and easily.

Here's my best, simplest definition: a closure is a kind of anonymous function that gets stored as a variable so it can be called later on, and has the special ability to remember the state of your program when you used it.

Some detail:

  • "Anonymous function": that is, a closure is a block of code you define, starting with { and ending with }. It's anonymous because it doesn't have a name – it doesn't need a name, because it gets stored as a variable.
  • "Stored as a variable": yes, the closure code literally gets saved as a variable, for example, myCode. Whoever is storing the closure (normally one of Apple's libraries) can then "call" that variable to run your closure's code.
  • "Called later on": once your closure has been stored away by iOS, it can be called a second later, a minute later, an hour later or never, depending on the situation. For example, when you say "run this code when my animation completes," iOS will make sure it happens at the right time.
  • "Remember the state of your program": if your closure references some variables that you had created, Swift will automatically take a copy of those variables so they can be used later. Remember, your closure can be called 20 minutes after you created it, so being able to store the original program state is important.

The truth is that you've probably used closures without realizing it. Even a simple UIView animation call uses closures for the animations, and optionally also for the completion block. Just think of it as a chunk of code that gets called later on, and you're most of the way there.

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure and A/B test your entire paywall UI without any code changes or app updates.

Learn more here

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

Available from iOS 7.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI 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 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 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.