Notice: Undefined index: understanding-swift in /var/sites/hackingwithswift.com/site/src/Controller/NewBooksReadController.php on line 89
When would closures with parameters be used as parameters? - a free tutorial

NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

When would closures with parameters be used as parameters?

Paul Hudson    @twostraws   

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 Instabug

SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.

Start your free trial!

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

BUY OUR BOOKS
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!

Link copied to your pasteboard.