NEW: Get your ticket to Hacking with Swift Live! >>


Day 6: Closures, part one

Brace yourself, because today we’re covering the first thing in Swift that many people have hard time understanding. Please keep in mind Flip Wilson's law: “you can't expect to hit the jackpot if you don't put a few nickels in the machine.”

Initially you’ll think that closures are simply anonymous functions – functions we can create and assign directly to a variable, then pass that variable around as if it were a string or an integer.

However, closures complicate things in two ways:

  • Their syntax can hurt your brain, and even explanations of their syntax can hurt your brain. For example, if a closure returns an integer, and you write a function that returns that closure, then you might read something like “this function returns a closure that returns an integer.” Yeah, I know – it’s hard.
  • If values used inside a closure were created outside the closure then Swift will make sure they remain available for the life of the closure so you don’t accidentally try to read something that doesn’t exist any more.

To make things easier to understand we start with fairly simple closures that might almost seem pointless, but as we progress on day two (yes, there’s a second day of closures!) you’ll start to see more advanced scenarios that build on what you learn today.

In case you were wondering: SwiftUI uses closures a lot, so this is something you should try to understand.

Today you have only five one-minute videos to watch, just to make sure you have time to watch videos twice if you need to. Once you’ve completed each video you can read the optional extra section, and there’s a short test to help make sure you’ve understood what was taught.

  1. Creating basic closures
  2. Accepting parameters in a closure
  3. Returning values from a closure
  4. Closures as parameters
  5. Trailing closure syntax

Sometimes people ask me “why do you start some topics by saying they are hard – aren’t you just putting people off?”

Obviously my goal is not to put you off Swift. Instead, my hope is that when you struggle with something you don’t ever think to yourself “I’m not cut out for Swift.” If you find closures hard it’s not because you aren’t smart enough – they are hard, so it’s just a sign your brain is working properly.

Don’t despair. Sometimes fighting to learn something makes it stick in your head better – there is no learning without struggle!

You can do this.

Still not sure about closures?

Hit Send below to start a virtual conversation with me.

Need help? Tweet me @twostraws!


Hacking with Swift is sponsored by Essential Developer

SPONSORED From August 2nd to 8th you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!

Save your spot now

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

100 Days of SwiftUI

The 100 Days of SwiftUI is a free collection of videos, tutorials, tests, and more to help you learn SwiftUI faster. Click here to learn more, or watch the video below.

Back to 100 Days of SwiftUI

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.