0

I am trying to follow the example in the Swift docs for a trailing closure.

This is the function:

func someFunctionThatTakesAClosure(closure: () -> Void) {
    // function body goes here
    print("we do something here and then go back")//does not print
}

And I call it here.

        print("about to call function")//prints ok
        someFunctionThatTakesAClosure(closure: {
            print("we did what was in the function and can now do something else")//does not print
        })
        print("after calling function")//prints ok

The function, however, is not getting called. What is wrong with the above?

Here's the Apple example:

func someFunctionThatTakesAClosure(closure: () -> Void) { // function body goes here }

// Here's how you call this function without using a trailing closure:

someFunctionThatTakesAClosure(closure: { // closure's body goes here })

1
  • A closure is just a value. Just like any other value, you can "ignore" its existence and nothing would happen. Making anything happen would require you to actually use the value. Commented Mar 23, 2019 at 0:27

2 Answers 2

1

Here is your example fixed:

func someFunctionThatTakesAClosure(closure: () -> Void) {
    // function body goes here
    print("we do something here and then go back")

    // don't forget to call the closure
    closure()
}


print("about to call function")

// call the function using trailing closure syntax
someFunctionThatTakesAClosure() {
    print("we did what was in the function and can now do something else")
}

print("after calling function")

Output:

about to call function
we do something here and then go back
we did what was in the function and can now do something else
after calling function
Sign up to request clarification or add additional context in comments.

Comments

1

Docs isn't very clear in explanation you need

print("1")
someFunctionThatTakesAClosure() {  // can be also  someFunctionThatTakesAClosure { without ()
    print("3") 

}

func someFunctionThatTakesAClosure(closure: () -> Void) { 
   print("2") 

   /// do you job here and line blow will get you back
    closure()
}  

the trailing closure is meant for completions like when you do a network request and finally return the response like this

func someFunctionThatTakesAClosure(completion:  @escaping ([String]) -> Void) { 
   print("inside the function body") 
   Api.getData { 
      completion(arr)
   }
}  

And to call

print("Before calling the function")
someFunctionThatTakesAClosure { (arr) in
  print("Inside the function callback  / trailing closure " , arr)
}
print("After calling the function") 

what you missed to read

enter image description here

1 Comment

Thanks, I saw that but read it as though you could use either one. Thx for clarifying! I upvoted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.