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

SOLVED: How to have trailing closure for function of type (first: () -> Void, second: String) -> Void

Forums > Swift

Hi! I was reading about trailing closures, and I have a doubt.

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

Can this function be called using trailing closure syntax?

   

@abbas asks:

Can this function be called using trailing closure syntax?

In @twoStraws' lesson on trailing closures he give an example similar to yours.

Here's a quote from his lesson:

If the last parameter to a function is a closure, Swift lets you use special
syntax called trailing closure syntax. Rather than pass in your closure as a parameter,
you pass it directly after the function inside braces.

Take a close look at your doImportantWork function's parameters. Look closer!

What is the last parameter of your function?

See -> Trailing Closure Syntax

1      

You can call this like

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

func printFirst() {
    print("First work done!")
}

doImportantWork(first: printFirst, second: "Second")

However to get a trailing closure you need to change the first so that it trailing

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

doImportantWork(second: "Second") {
    print("First work done!")
}

You can even have multiples trailing closures

func doImportantWork(first: () -> Void, second: (String) -> Void) {
    print("About to start first work")
    first()
    print("About to start second work")
    second("Second")
}

doImportantWork {
    print("First work done!")
} second: { string in
    print("\(string) work done!")
}

or even more

func doImportantWork(first: () -> Void, second: (String) -> Void, third: (Int, String) -> Void) {
    print("About to start first work")
    first()
    print("About to start second work")
    second("Second")
    print("About to start third work")
    third(3, "Third")
}

doImportantWork {
    print("First work done!")
} second: { string in
    print("\(string) work done!")
} third: { number, string in
    print("\(number). \(string) work done!")
}

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.