Different types of closure syntax in swift - which one is correct?











up vote
3
down vote

favorite












I'm pretty curious which one of these syntax statements is (more) correct.
Playground happily compiles both cases.



Method 1



// copied from SO and this appears clear to me
UIView.animate(
withDuration: 3.0,
animations: {

},
completion: { (Bool) in
// completion code
}
)


Method 2



UIView.animate(
withDuration: 3.0,
animations: {

// code

}) {(Bool) in
// code when finished?
// argument label completion missing?
}


Why rounded brackets in 2nd method are closed before last argument stated? Or is that another implementation of UIView.animation?










share|improve this question


























    up vote
    3
    down vote

    favorite












    I'm pretty curious which one of these syntax statements is (more) correct.
    Playground happily compiles both cases.



    Method 1



    // copied from SO and this appears clear to me
    UIView.animate(
    withDuration: 3.0,
    animations: {

    },
    completion: { (Bool) in
    // completion code
    }
    )


    Method 2



    UIView.animate(
    withDuration: 3.0,
    animations: {

    // code

    }) {(Bool) in
    // code when finished?
    // argument label completion missing?
    }


    Why rounded brackets in 2nd method are closed before last argument stated? Or is that another implementation of UIView.animation?










    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I'm pretty curious which one of these syntax statements is (more) correct.
      Playground happily compiles both cases.



      Method 1



      // copied from SO and this appears clear to me
      UIView.animate(
      withDuration: 3.0,
      animations: {

      },
      completion: { (Bool) in
      // completion code
      }
      )


      Method 2



      UIView.animate(
      withDuration: 3.0,
      animations: {

      // code

      }) {(Bool) in
      // code when finished?
      // argument label completion missing?
      }


      Why rounded brackets in 2nd method are closed before last argument stated? Or is that another implementation of UIView.animation?










      share|improve this question













      I'm pretty curious which one of these syntax statements is (more) correct.
      Playground happily compiles both cases.



      Method 1



      // copied from SO and this appears clear to me
      UIView.animate(
      withDuration: 3.0,
      animations: {

      },
      completion: { (Bool) in
      // completion code
      }
      )


      Method 2



      UIView.animate(
      withDuration: 3.0,
      animations: {

      // code

      }) {(Bool) in
      // code when finished?
      // argument label completion missing?
      }


      Why rounded brackets in 2nd method are closed before last argument stated? Or is that another implementation of UIView.animation?







      ios swift closures swift3 completionhandler






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 22 '16 at 15:21









      GiorgioE

      129110




      129110
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          The difference between both methods is following:



          Method 1: Regular closure



          Method 2: Trailing closure.
          Last closure parameter in the signature of a function can be written in shorter syntax. If the second parameter would be completion, and animations parameter would be the last, the trailing closure would apply to animations etc.
          So it has to stand as the last (or the only) closure parameter.



          If you miss a completion label, you are free to type it like this:



          UIView.animate(withDuration: 3.0, animations: {

          }) {(completion: Bool) in

          }


          For completion of your question as well: It is the same implementation of an identical function, but a different syntax.






          share|improve this answer



















          • 1




            Was going to say that, but you stated it nicely. The advantage of using the trailing closure is that it often results in cleaner looking code.
            – picciano
            Sep 22 '16 at 15:27










          • Well after I took a look again, it appeared to me cleaner looking too :) Thank you.
            – GiorgioE
            Sep 22 '16 at 15:40


















          up vote
          6
          down vote













          Both of them are correct.




          1. It is the usual closure syntax in a function call.


          2. It represents a Trailing closure.




          If you need to pass a closure expression to a function as the
          function’s final argument and the closure expression is long, it can
          be useful to write it as a trailing closure instead. A trailing
          closure is written after the function call’s parentheses, even though
          it is still an argument to the function. When you use the trailing
          closure syntax, you don’t write the argument label for the closure as
          part of the function call.




          You can read more about trailing closures from https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html






          share|improve this answer

















          • 1




            Thank you, so we shouldn't write arg. label within trailing closure? Or just it is not needed?
            – GiorgioE
            Sep 22 '16 at 15:39








          • 2




            There is no scope for writing argument label with trailing closure. "completion:" need not be written. The body of the trailing closure is written outside the method calling brackets i.e UIView.animate(...){trailing closure body}
            – PGDev
            Sep 22 '16 at 15:56











          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',
          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%2f39642965%2fdifferent-types-of-closure-syntax-in-swift-which-one-is-correct%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








          up vote
          4
          down vote



          accepted










          The difference between both methods is following:



          Method 1: Regular closure



          Method 2: Trailing closure.
          Last closure parameter in the signature of a function can be written in shorter syntax. If the second parameter would be completion, and animations parameter would be the last, the trailing closure would apply to animations etc.
          So it has to stand as the last (or the only) closure parameter.



          If you miss a completion label, you are free to type it like this:



          UIView.animate(withDuration: 3.0, animations: {

          }) {(completion: Bool) in

          }


          For completion of your question as well: It is the same implementation of an identical function, but a different syntax.






          share|improve this answer



















          • 1




            Was going to say that, but you stated it nicely. The advantage of using the trailing closure is that it often results in cleaner looking code.
            – picciano
            Sep 22 '16 at 15:27










          • Well after I took a look again, it appeared to me cleaner looking too :) Thank you.
            – GiorgioE
            Sep 22 '16 at 15:40















          up vote
          4
          down vote



          accepted










          The difference between both methods is following:



          Method 1: Regular closure



          Method 2: Trailing closure.
          Last closure parameter in the signature of a function can be written in shorter syntax. If the second parameter would be completion, and animations parameter would be the last, the trailing closure would apply to animations etc.
          So it has to stand as the last (or the only) closure parameter.



          If you miss a completion label, you are free to type it like this:



          UIView.animate(withDuration: 3.0, animations: {

          }) {(completion: Bool) in

          }


          For completion of your question as well: It is the same implementation of an identical function, but a different syntax.






          share|improve this answer



















          • 1




            Was going to say that, but you stated it nicely. The advantage of using the trailing closure is that it often results in cleaner looking code.
            – picciano
            Sep 22 '16 at 15:27










          • Well after I took a look again, it appeared to me cleaner looking too :) Thank you.
            – GiorgioE
            Sep 22 '16 at 15:40













          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          The difference between both methods is following:



          Method 1: Regular closure



          Method 2: Trailing closure.
          Last closure parameter in the signature of a function can be written in shorter syntax. If the second parameter would be completion, and animations parameter would be the last, the trailing closure would apply to animations etc.
          So it has to stand as the last (or the only) closure parameter.



          If you miss a completion label, you are free to type it like this:



          UIView.animate(withDuration: 3.0, animations: {

          }) {(completion: Bool) in

          }


          For completion of your question as well: It is the same implementation of an identical function, but a different syntax.






          share|improve this answer














          The difference between both methods is following:



          Method 1: Regular closure



          Method 2: Trailing closure.
          Last closure parameter in the signature of a function can be written in shorter syntax. If the second parameter would be completion, and animations parameter would be the last, the trailing closure would apply to animations etc.
          So it has to stand as the last (or the only) closure parameter.



          If you miss a completion label, you are free to type it like this:



          UIView.animate(withDuration: 3.0, animations: {

          }) {(completion: Bool) in

          }


          For completion of your question as well: It is the same implementation of an identical function, but a different syntax.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 22 '16 at 15:37

























          answered Sep 22 '16 at 15:25









          pedrouan

          9,17124060




          9,17124060








          • 1




            Was going to say that, but you stated it nicely. The advantage of using the trailing closure is that it often results in cleaner looking code.
            – picciano
            Sep 22 '16 at 15:27










          • Well after I took a look again, it appeared to me cleaner looking too :) Thank you.
            – GiorgioE
            Sep 22 '16 at 15:40














          • 1




            Was going to say that, but you stated it nicely. The advantage of using the trailing closure is that it often results in cleaner looking code.
            – picciano
            Sep 22 '16 at 15:27










          • Well after I took a look again, it appeared to me cleaner looking too :) Thank you.
            – GiorgioE
            Sep 22 '16 at 15:40








          1




          1




          Was going to say that, but you stated it nicely. The advantage of using the trailing closure is that it often results in cleaner looking code.
          – picciano
          Sep 22 '16 at 15:27




          Was going to say that, but you stated it nicely. The advantage of using the trailing closure is that it often results in cleaner looking code.
          – picciano
          Sep 22 '16 at 15:27












          Well after I took a look again, it appeared to me cleaner looking too :) Thank you.
          – GiorgioE
          Sep 22 '16 at 15:40




          Well after I took a look again, it appeared to me cleaner looking too :) Thank you.
          – GiorgioE
          Sep 22 '16 at 15:40












          up vote
          6
          down vote













          Both of them are correct.




          1. It is the usual closure syntax in a function call.


          2. It represents a Trailing closure.




          If you need to pass a closure expression to a function as the
          function’s final argument and the closure expression is long, it can
          be useful to write it as a trailing closure instead. A trailing
          closure is written after the function call’s parentheses, even though
          it is still an argument to the function. When you use the trailing
          closure syntax, you don’t write the argument label for the closure as
          part of the function call.




          You can read more about trailing closures from https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html






          share|improve this answer

















          • 1




            Thank you, so we shouldn't write arg. label within trailing closure? Or just it is not needed?
            – GiorgioE
            Sep 22 '16 at 15:39








          • 2




            There is no scope for writing argument label with trailing closure. "completion:" need not be written. The body of the trailing closure is written outside the method calling brackets i.e UIView.animate(...){trailing closure body}
            – PGDev
            Sep 22 '16 at 15:56















          up vote
          6
          down vote













          Both of them are correct.




          1. It is the usual closure syntax in a function call.


          2. It represents a Trailing closure.




          If you need to pass a closure expression to a function as the
          function’s final argument and the closure expression is long, it can
          be useful to write it as a trailing closure instead. A trailing
          closure is written after the function call’s parentheses, even though
          it is still an argument to the function. When you use the trailing
          closure syntax, you don’t write the argument label for the closure as
          part of the function call.




          You can read more about trailing closures from https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html






          share|improve this answer

















          • 1




            Thank you, so we shouldn't write arg. label within trailing closure? Or just it is not needed?
            – GiorgioE
            Sep 22 '16 at 15:39








          • 2




            There is no scope for writing argument label with trailing closure. "completion:" need not be written. The body of the trailing closure is written outside the method calling brackets i.e UIView.animate(...){trailing closure body}
            – PGDev
            Sep 22 '16 at 15:56













          up vote
          6
          down vote










          up vote
          6
          down vote









          Both of them are correct.




          1. It is the usual closure syntax in a function call.


          2. It represents a Trailing closure.




          If you need to pass a closure expression to a function as the
          function’s final argument and the closure expression is long, it can
          be useful to write it as a trailing closure instead. A trailing
          closure is written after the function call’s parentheses, even though
          it is still an argument to the function. When you use the trailing
          closure syntax, you don’t write the argument label for the closure as
          part of the function call.




          You can read more about trailing closures from https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html






          share|improve this answer












          Both of them are correct.




          1. It is the usual closure syntax in a function call.


          2. It represents a Trailing closure.




          If you need to pass a closure expression to a function as the
          function’s final argument and the closure expression is long, it can
          be useful to write it as a trailing closure instead. A trailing
          closure is written after the function call’s parentheses, even though
          it is still an argument to the function. When you use the trailing
          closure syntax, you don’t write the argument label for the closure as
          part of the function call.




          You can read more about trailing closures from https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 22 '16 at 15:29









          PGDev

          6,33721145




          6,33721145








          • 1




            Thank you, so we shouldn't write arg. label within trailing closure? Or just it is not needed?
            – GiorgioE
            Sep 22 '16 at 15:39








          • 2




            There is no scope for writing argument label with trailing closure. "completion:" need not be written. The body of the trailing closure is written outside the method calling brackets i.e UIView.animate(...){trailing closure body}
            – PGDev
            Sep 22 '16 at 15:56














          • 1




            Thank you, so we shouldn't write arg. label within trailing closure? Or just it is not needed?
            – GiorgioE
            Sep 22 '16 at 15:39








          • 2




            There is no scope for writing argument label with trailing closure. "completion:" need not be written. The body of the trailing closure is written outside the method calling brackets i.e UIView.animate(...){trailing closure body}
            – PGDev
            Sep 22 '16 at 15:56








          1




          1




          Thank you, so we shouldn't write arg. label within trailing closure? Or just it is not needed?
          – GiorgioE
          Sep 22 '16 at 15:39






          Thank you, so we shouldn't write arg. label within trailing closure? Or just it is not needed?
          – GiorgioE
          Sep 22 '16 at 15:39






          2




          2




          There is no scope for writing argument label with trailing closure. "completion:" need not be written. The body of the trailing closure is written outside the method calling brackets i.e UIView.animate(...){trailing closure body}
          – PGDev
          Sep 22 '16 at 15:56




          There is no scope for writing argument label with trailing closure. "completion:" need not be written. The body of the trailing closure is written outside the method calling brackets i.e UIView.animate(...){trailing closure body}
          – PGDev
          Sep 22 '16 at 15:56


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f39642965%2fdifferent-types-of-closure-syntax-in-swift-which-one-is-correct%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







          這個網誌中的熱門文章

          Academy of Television Arts & Sciences

          L'Équipe

          1995 France bombings