Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: Closures

13. Challenge: Closure Syntax

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 12. Closure Syntax Next episode: 14. forEach & map

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 13. Challenge: Closure Syntax

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Transcript: 13. Challenge: Closure Syntax

It is challenge time again! You’ll find it on page 5 of the Playground for this part of the course. Pause the video, practice writing closures a few different ways, and then come back to check your work!

Hey! Welcome back.Your challenge this time was to write alternate versions of this closure. If you were ever punished in school by having to copy lines over and over again, this closure will help you out! It’s using a String initializer that will repeat a string a specific number of times.

First, a closure with no parameter types. I’ll copy the original closure and paste it below, and just put an “_2” after the name.

let copyLines😺_2🛑 =

Then, before I can shorten it up, I need to explicitly type the closure. This one takes a String and an Int, and returns nothing.

let copyLines_2😺: (String, Int) -> Void🛑 = 

Now I can use type inference inside the closure! So, I’ll just delete both of these types from the parameter list.

let copyLines_2: (String, Int) -> Void = { (offense❌: String❌, repeatCount❌: Int❌) -> Void in

Also, you don’t actually need these parentheses around the parameters if you aren’t specifying the types there. They can make things more readable, but the closure will work without them. So give that a try, too!

 = { ❌(❌offense, repeatCount❌)❌ -> Void in

Next! No parameter or return types… OK, copy this second closure. Paste it, and just change this 2 to a 3.

let copyLines_3:

And its already set up to infer the return type, too, so I can just remove this return Void.

 = { offense, repeatCount❌ -> Void❌ in

And for the last one, copy and paste one more time…

let copyLines_4: 

For this one, I need to get rid of the parameter names.

= { ❌offense, repeatCount❌ in

Since I don’t have those names to refer to anymore, I need to update the body of the closure to use $0 and $1

  print( String(repeating: "I must not \(😺$0).", count: 😺$1) )

And I’m done! I’ll call all four versions of this closure with the same offense…

copyLines("tell lies", 5)
copyLines_2("tell lies", 5)
copyLines_3("tell lies", 5)
copyLines_4("tell lies", 5)

Run the playground and there in the console, just like Harry Potter, we must not tell lies!