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

DAY 9

Closures


Brace yourself, because today we’re covering the first thing in Swift that many people have a 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.”

Today the topic is closures, which are a bit like anonymous functions – functions we can create and assign directly to a variable, or pass into other functions to customize how they work. Yes, you read that right: passing one function into another as a parameter.

Closures are really difficult. I’m not saying that to put you off, only so that you know in advance if you’re finding closures hard to understand or hard to remember, it’s okay – we’ve all been there!

Sometimes the syntax for closures can be a bit hard on your eyes, and this will really be apparent as you work through today’s lessons. If you find it a bit overwhelming – if you’re staring at some code and aren’t 100% sure of what it means – just go back one video and watch it again to give your memory a little nudge. You’ll find there are more tests and optional reading links than usual below, hopefully helping to solidify your knowledge.

SwiftUI uses closures extensively so it’s worth taking the time to understand what’s going on here. Yes, closures are probably the most complex feature of Swift, but it’s a bit like cycling up a hill – once you’ve reached the top, once you’ve mastered closures, it all gets much easier.

Today you have three tutorials to follow, plus a summary and another checkpoint. As always once you’ve completed each video there’s some optional extra reading and short tests to help make sure you’ve understood what was taught. This time you’ll notice there’s quite a bit of each of those because closures really can take some time to understand, so don’t be afraid to explore!

  1. How to create and use closures
  2. How to use trailing closures and shorthand syntax
  3. How to accept functions as parameters
  4. Summary: Closures
  5. Checkpoint 5

That’s another key Swift topic under your belt – good job! Now do the smart thing and post your progress online: it forces you to write about these things in your own words, and also encourages you to continue on to tomorrow.

Still not sure about closures?

Hit Send below to start a virtual conversation with me.

Share your progress!

If you use Twitter, the button below will prepare a tweet saying you completed today, along with a celebratory graphic, the URL to this page, and the challenge hashtag. Don't worry – it won't be sent until you confirm on Twitter!

Need help? Tweet me @twostraws!

 

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.