NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Day 9 - accept functions as parameters course question

Forums > 100 Days of SwiftUI

Hey! It's the wonderpig again 😅

I'm following day 9 on the accepting functions as parameters but I dont understand the last example with the "doImportantWork" function.

I followed quite nicely up until that point but I don't really understand what the code he makes us write does or even why I should care about this.

If I understand the bellow code is creating a function that contain functions returning nothing as parameters.

func doImportantWork(first: () -> Void, second: () -> Void, third: () -> Void) {
    print("About to start first work")
    first()
    print("About to start second work")
    second()
    print("About to start third work")
    third()
    print("Done!")
}

But then the second part of the code is doing what? Defining what the enclosed functions (first, second and third) do? I'm kinda lost here 🤣

doImportantWork {
    print("This is the first work")
} second: {
    print("This is the second work")
} third: {
    print("This is the third work")
}

Has any of you understood this part of the lesson?

Thanks!

   

Think of it as a list of tasks you have to do to process something. That function accepts three another functions and does something with them. But before each of the passed functions, let's say you need to do some extra work. So before you handle first function passed, you need to do something, prepare data, make calculations... etc. Using an analogy you can imagine function prepare hot beverage. That function accepts other function: open a box of tea leaves, but before you open that box and do something with it you need to boil water... etc... so it will look something like this

func makeHotBeverage(firstStep: () -> Void ...) { 
  boilWater() 
  firstStep()
  ...
}

when you call that function it will look like this:

makeHotBeverage {
    openBoxOfTeaLeaves()
}

you use openBoxOfTeaLeaves() here but this function signature i.e. its type is () -> Void. If next time you want to make coffe you can use it. So it becomes reusable. Boiled water is necessary for coffee too...

makeHotBeverage {
    // Water is boiled prior next step is executed as you put that in function declaration.
    openBoxOfCoffeeBeans()
    ....
}

Paul is giving simple print statements in order to avoid writing such long code with different functions. You need to undestandand that you can pass functions in to other functions and order them in certain way as well you can use them as closures.

   

Ok I think I get it a bit better now. Thanks a lot friend 😊

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.