Super simple trailing closure syntax in SwiftHow do JavaScript closures work?What is the difference between a 'closure' and a 'lambda'?How can I remove a trailing newline in Python?JavaScript closure inside loops – simple practical exampleJavaScript closures vs. anonymous functions#pragma mark in Swift?Swift Beta performance: sorting arraysDont understand this Trailing ClosureSimple closure example with SwiftEquivalent of Swift trailing closure in C++

Will 700 more planes a day fly because of the Heathrow expansion?

What was the first story to feature the plot "the monsters were human all along"?

Adding command shortcuts to bin

Where can I go to avoid planes overhead?

Decoupling cap routing on a 4 layer PCB

Refinish or replace an old staircase

Are the Night's Watch still required?

What does 'made on' mean here?

What is a smasher?

In Russian, how do you idiomatically express the idea of the figurative "overnight"?

How can I roleplay a follower-type character when I as a player have a leader-type personality?

Why wasn't the Night King naked in S08E03?

How do LIGO and VIRGO know that a gravitational wave has its origin in a neutron star or a black hole?

Why does sound not move through a wall?

How to safely wipe a USB flash drive

How to adjust tikz picture so it fits to current size of a table cell?

Should I dumb down my writing in a foreign country?

Identifying characters

How did the Venus Express detect lightning?

I need a disease

Are Finitely generated modules over a ring also finitely generated over a subring containing the identity?

Is there an official reason for not adding a post-credits scene?

Should I decline this job offer that requires relocating to an area with high cost of living?

29er Road Tire?



Super simple trailing closure syntax in Swift


How do JavaScript closures work?What is the difference between a 'closure' and a 'lambda'?How can I remove a trailing newline in Python?JavaScript closure inside loops – simple practical exampleJavaScript closures vs. anonymous functions#pragma mark in Swift?Swift Beta performance: sorting arraysDont understand this Trailing ClosureSimple closure example with SwiftEquivalent of Swift trailing closure in C++






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








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 )











share|improve this question






















  • 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.

    – Alexander
    Mar 23 at 0:27

















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 )











share|improve this question






















  • 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.

    – Alexander
    Mar 23 at 0:27













0












0








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 )











share|improve this question














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 )








ios swift closures trailing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 23:58









user6631314user6631314

626413




626413












  • 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.

    – Alexander
    Mar 23 at 0:27

















  • 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.

    – Alexander
    Mar 23 at 0:27
















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.

– Alexander
Mar 23 at 0:27





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.

– Alexander
Mar 23 at 0:27












2 Answers
2






active

oldest

votes


















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






share|improve this answer






























    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






    share|improve this answer

























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

      – user6631314
      Mar 23 at 2:34












    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55309237%2fsuper-simple-trailing-closure-syntax-in-swift%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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






    share|improve this answer



























      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






      share|improve this answer

























        1












        1








        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 23 at 0:19









        vacawamavacawama

        101k15179206




        101k15179206























            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






            share|improve this answer

























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

              – user6631314
              Mar 23 at 2:34
















            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






            share|improve this answer

























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

              – user6631314
              Mar 23 at 2:34














            1












            1








            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






            share|improve this answer















            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







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 23 at 0:46

























            answered Mar 23 at 0:09









            Sh_KhanSh_Khan

            49.6k51434




            49.6k51434












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

              – user6631314
              Mar 23 at 2:34


















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

              – user6631314
              Mar 23 at 2:34

















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

            – user6631314
            Mar 23 at 2:34






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

            – user6631314
            Mar 23 at 2:34


















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55309237%2fsuper-simple-trailing-closure-syntax-in-swift%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript