UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

When would closures with parameters be used as parameters?

Paul Hudson    @twostraws   

Updated for Xcode 15

Closures that accept parameters are a little hard to read, but that problem gets worse when the closures themselves are used as parameters – it feels almost like an IQ test just trying to understand what’s going on. However, they are commonly used so it’s worth taking the time to understand the way they look and the way they work – and if you find them a little overwhelming, don’t be afraid to go back a little and review what came before.

To give you a practical example, imagine you were building a car. The car needs to know what engine it has, what steering wheel it has, how many seats it has, and so on. Sure, the engine could just be a string of information, but really it should be able to actually accelerate or decelerate to a certain speed.

So, first we might write a closure like this one:

let changeSpeed = { (speed: Int) in
    print("Changing speed to \(speed)kph")
}

And now we can create a buildCar() function that accepts any sort of closure for the engine, as long as that closure can be told to accelerate or decelerate to a specific integer value:

func buildCar(name: String, engine: (Int) -> Void) {
    // build the car
}

Once you become more comfortable with closures, you’ll start to realize the power here is that our closure is effectively a sealed box. Yes, we know that it will print a message that we’re changing speed, but all buildCar() knows is that it takes an integer and returns nothing. We could create a completely different closure to handle flying cars or underwater cars, and buildCar() wouldn’t care because it satisfies the same rules of accepting an integer and returning nothing.

Before I finish, I want to repeat an earlier warning, because this is where closures can really start to hurt your head:

If you’re sitting here thinking “wow, closures are really hard”, don’t be dismayed – they are hard, and you finding them hard just means your brain is operating correctly.

I get so many messages from folks saying they are struggling with closures, and I say exactly the same thing: it’s okay. It’s better than okay – it’s great! When you struggle to learn information you’ll find it sinks in way more, so when you finally master it you’ll be set.

And trust me: this stuff matters, because it’s used all over the place in SwiftUI. So, take your time and keep going!

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 your entire paywall view without any code changes or app updates.

Learn more here

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

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: 4.4/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.