Syntax of Block in Swift





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







14















I am trying to rewrite from Objective-C to Swift, I cannot work out the syntax or understand the docs



Here is a simplified example in Objective-C I wrote:



[UIView animateWithDuration:10.0 animations:^{self.navigationController.toolbar.frame = CGRectMake(0,10,0,10);}];


How do I write this in Swift?



This is the template autocomplete gives:



UIView.animateWithDuration(duration: NSTimeInterval, animations: (() -> Void))









share|improve this question































    14















    I am trying to rewrite from Objective-C to Swift, I cannot work out the syntax or understand the docs



    Here is a simplified example in Objective-C I wrote:



    [UIView animateWithDuration:10.0 animations:^{self.navigationController.toolbar.frame = CGRectMake(0,10,0,10);}];


    How do I write this in Swift?



    This is the template autocomplete gives:



    UIView.animateWithDuration(duration: NSTimeInterval, animations: (() -> Void))









    share|improve this question



























      14












      14








      14


      2






      I am trying to rewrite from Objective-C to Swift, I cannot work out the syntax or understand the docs



      Here is a simplified example in Objective-C I wrote:



      [UIView animateWithDuration:10.0 animations:^{self.navigationController.toolbar.frame = CGRectMake(0,10,0,10);}];


      How do I write this in Swift?



      This is the template autocomplete gives:



      UIView.animateWithDuration(duration: NSTimeInterval, animations: (() -> Void))









      share|improve this question
















      I am trying to rewrite from Objective-C to Swift, I cannot work out the syntax or understand the docs



      Here is a simplified example in Objective-C I wrote:



      [UIView animateWithDuration:10.0 animations:^{self.navigationController.toolbar.frame = CGRectMake(0,10,0,10);}];


      How do I write this in Swift?



      This is the template autocomplete gives:



      UIView.animateWithDuration(duration: NSTimeInterval, animations: (() -> Void))






      ios swift swift3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 8:08









      Martijn Pieters

      726k14325482349




      726k14325482349










      asked Jun 4 '14 at 13:25









      Ryan HeitnerRyan Heitner

      8,43155691




      8,43155691
























          4 Answers
          4






          active

          oldest

          votes


















          10














          Since the expected argument types and return type to the animations argument are known the compiler can infer them without a problem. This should work (though I don't have the playground available right at the moment:



          UIView.animateWithDuration(10.0, animations: {
          self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
          })


          for more info about closures see the chapter in the swift docs



          note about CGRect() - the developer docs show CGRect() being used in swift code. Perhaps it requires an import?



          update for comments: you can also use a trailing closure like so:



          UIView.animateWithDuration(10.0) {
          self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
          }





          share|improve this answer





















          • 1





            maybe a trailing closure would be even simpler? :)

            – Sulthan
            Jun 4 '14 at 13:29






          • 2





            @Jiaaro Named arguments are not a problem. It must be the last argument, that's all.

            – Sulthan
            Jun 4 '14 at 13:34











          • Is it just me or is trailing closures much more confusing? Does anyone know what language this was influenced by or why this makes sense?

            – sanz
            Jun 4 '14 at 14:24











          • Is this to allow for curry operations? Seems like a obtuse way to do it...

            – sanz
            Jun 4 '14 at 14:28











          • It allows callbacks to "read like control flow"

            – Jiaaro
            Jun 4 '14 at 14:39



















          16














          This is the swift closure format:



          {(parameter:type, parameter: type, ...) -> returntype in
          //do stuff
          }


          This is what you should do:



          //The animation closure will take no parameters and return void (nothing).
          UIView.animateWithDuration(duration: NSTimeInterval, animations: {() -> Void in
          //Animate anything.
          })


          Here is the documentation for closures.






          share|improve this answer

































            5














            Following code can guide to write your own block.



            class func testFunc(completion: ((list : NSArray!) -> Void)?) {
            //--- block code.
            if completion! != nil {
            completion! (list: NSArray())
            }
            }


            and you can call it like -



            className.testFunc {
            (list: NSArray!) -> Void in
            }





            share|improve this answer































              3














              You can basically write it in 3 identical ways:



              write what to do right in the closure/code block:



              UIView.animateWithDuration(10.0) {
              self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
              }


              This is also known as trailing closure ( You can only do trailing closure if the closure parameter is the last parameter)



              This doesn't mean the parameter 'animations' is no longer written. It is written but just as in the format of above.





              Write exactly within the lines, most developers avoid such, because it's a little buggy to write with all the parenthesis and braces.



              UIView.animateWithDuration(10.0, animations: {
              self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
              })


              (Contrary to trailing closure you wrote name ie 'animations')
              This is known as inline closure





              Write in a more modular sense



              UIView.animateWithDuration(duration: NSTimeInterval, animations: animatingFunc)

              func animatingFunc() {
              self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
              }


              Remember the type of the parameter 'animations' was () -> Void



              Exactly as what we are doing, animatingFunc takes no parameters ie '()' and returns nothing ie 'void'



              (In Swift, functions are types and can be passed in as parameters)
              Some might say this is more readable some might say trailing closure is...





              Side note1
              You can also do nothing ( which really doesn't make sense but in many other handlers/animations/completion handlers you may not want to do anything)



              UIView.animateWithDuration(duration: NSTimeInterval, animations: nil)




              Side note2



              Closures becomes more interesting when you have to capture a value. See this simple demonstration.
              For more information about Swift closures see Apple's Documentation






              share|improve this answer


























                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%2f24038713%2fsyntax-of-block-in-swift%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                10














                Since the expected argument types and return type to the animations argument are known the compiler can infer them without a problem. This should work (though I don't have the playground available right at the moment:



                UIView.animateWithDuration(10.0, animations: {
                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                })


                for more info about closures see the chapter in the swift docs



                note about CGRect() - the developer docs show CGRect() being used in swift code. Perhaps it requires an import?



                update for comments: you can also use a trailing closure like so:



                UIView.animateWithDuration(10.0) {
                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                }





                share|improve this answer





















                • 1





                  maybe a trailing closure would be even simpler? :)

                  – Sulthan
                  Jun 4 '14 at 13:29






                • 2





                  @Jiaaro Named arguments are not a problem. It must be the last argument, that's all.

                  – Sulthan
                  Jun 4 '14 at 13:34











                • Is it just me or is trailing closures much more confusing? Does anyone know what language this was influenced by or why this makes sense?

                  – sanz
                  Jun 4 '14 at 14:24











                • Is this to allow for curry operations? Seems like a obtuse way to do it...

                  – sanz
                  Jun 4 '14 at 14:28











                • It allows callbacks to "read like control flow"

                  – Jiaaro
                  Jun 4 '14 at 14:39
















                10














                Since the expected argument types and return type to the animations argument are known the compiler can infer them without a problem. This should work (though I don't have the playground available right at the moment:



                UIView.animateWithDuration(10.0, animations: {
                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                })


                for more info about closures see the chapter in the swift docs



                note about CGRect() - the developer docs show CGRect() being used in swift code. Perhaps it requires an import?



                update for comments: you can also use a trailing closure like so:



                UIView.animateWithDuration(10.0) {
                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                }





                share|improve this answer





















                • 1





                  maybe a trailing closure would be even simpler? :)

                  – Sulthan
                  Jun 4 '14 at 13:29






                • 2





                  @Jiaaro Named arguments are not a problem. It must be the last argument, that's all.

                  – Sulthan
                  Jun 4 '14 at 13:34











                • Is it just me or is trailing closures much more confusing? Does anyone know what language this was influenced by or why this makes sense?

                  – sanz
                  Jun 4 '14 at 14:24











                • Is this to allow for curry operations? Seems like a obtuse way to do it...

                  – sanz
                  Jun 4 '14 at 14:28











                • It allows callbacks to "read like control flow"

                  – Jiaaro
                  Jun 4 '14 at 14:39














                10












                10








                10







                Since the expected argument types and return type to the animations argument are known the compiler can infer them without a problem. This should work (though I don't have the playground available right at the moment:



                UIView.animateWithDuration(10.0, animations: {
                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                })


                for more info about closures see the chapter in the swift docs



                note about CGRect() - the developer docs show CGRect() being used in swift code. Perhaps it requires an import?



                update for comments: you can also use a trailing closure like so:



                UIView.animateWithDuration(10.0) {
                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                }





                share|improve this answer















                Since the expected argument types and return type to the animations argument are known the compiler can infer them without a problem. This should work (though I don't have the playground available right at the moment:



                UIView.animateWithDuration(10.0, animations: {
                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                })


                for more info about closures see the chapter in the swift docs



                note about CGRect() - the developer docs show CGRect() being used in swift code. Perhaps it requires an import?



                update for comments: you can also use a trailing closure like so:



                UIView.animateWithDuration(10.0) {
                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jun 4 '14 at 13:45









                Gabriele Petronella

                92.3k18181213




                92.3k18181213










                answered Jun 4 '14 at 13:28









                JiaaroJiaaro

                46.3k32135171




                46.3k32135171








                • 1





                  maybe a trailing closure would be even simpler? :)

                  – Sulthan
                  Jun 4 '14 at 13:29






                • 2





                  @Jiaaro Named arguments are not a problem. It must be the last argument, that's all.

                  – Sulthan
                  Jun 4 '14 at 13:34











                • Is it just me or is trailing closures much more confusing? Does anyone know what language this was influenced by or why this makes sense?

                  – sanz
                  Jun 4 '14 at 14:24











                • Is this to allow for curry operations? Seems like a obtuse way to do it...

                  – sanz
                  Jun 4 '14 at 14:28











                • It allows callbacks to "read like control flow"

                  – Jiaaro
                  Jun 4 '14 at 14:39














                • 1





                  maybe a trailing closure would be even simpler? :)

                  – Sulthan
                  Jun 4 '14 at 13:29






                • 2





                  @Jiaaro Named arguments are not a problem. It must be the last argument, that's all.

                  – Sulthan
                  Jun 4 '14 at 13:34











                • Is it just me or is trailing closures much more confusing? Does anyone know what language this was influenced by or why this makes sense?

                  – sanz
                  Jun 4 '14 at 14:24











                • Is this to allow for curry operations? Seems like a obtuse way to do it...

                  – sanz
                  Jun 4 '14 at 14:28











                • It allows callbacks to "read like control flow"

                  – Jiaaro
                  Jun 4 '14 at 14:39








                1




                1





                maybe a trailing closure would be even simpler? :)

                – Sulthan
                Jun 4 '14 at 13:29





                maybe a trailing closure would be even simpler? :)

                – Sulthan
                Jun 4 '14 at 13:29




                2




                2





                @Jiaaro Named arguments are not a problem. It must be the last argument, that's all.

                – Sulthan
                Jun 4 '14 at 13:34





                @Jiaaro Named arguments are not a problem. It must be the last argument, that's all.

                – Sulthan
                Jun 4 '14 at 13:34













                Is it just me or is trailing closures much more confusing? Does anyone know what language this was influenced by or why this makes sense?

                – sanz
                Jun 4 '14 at 14:24





                Is it just me or is trailing closures much more confusing? Does anyone know what language this was influenced by or why this makes sense?

                – sanz
                Jun 4 '14 at 14:24













                Is this to allow for curry operations? Seems like a obtuse way to do it...

                – sanz
                Jun 4 '14 at 14:28





                Is this to allow for curry operations? Seems like a obtuse way to do it...

                – sanz
                Jun 4 '14 at 14:28













                It allows callbacks to "read like control flow"

                – Jiaaro
                Jun 4 '14 at 14:39





                It allows callbacks to "read like control flow"

                – Jiaaro
                Jun 4 '14 at 14:39













                16














                This is the swift closure format:



                {(parameter:type, parameter: type, ...) -> returntype in
                //do stuff
                }


                This is what you should do:



                //The animation closure will take no parameters and return void (nothing).
                UIView.animateWithDuration(duration: NSTimeInterval, animations: {() -> Void in
                //Animate anything.
                })


                Here is the documentation for closures.






                share|improve this answer






























                  16














                  This is the swift closure format:



                  {(parameter:type, parameter: type, ...) -> returntype in
                  //do stuff
                  }


                  This is what you should do:



                  //The animation closure will take no parameters and return void (nothing).
                  UIView.animateWithDuration(duration: NSTimeInterval, animations: {() -> Void in
                  //Animate anything.
                  })


                  Here is the documentation for closures.






                  share|improve this answer




























                    16












                    16








                    16







                    This is the swift closure format:



                    {(parameter:type, parameter: type, ...) -> returntype in
                    //do stuff
                    }


                    This is what you should do:



                    //The animation closure will take no parameters and return void (nothing).
                    UIView.animateWithDuration(duration: NSTimeInterval, animations: {() -> Void in
                    //Animate anything.
                    })


                    Here is the documentation for closures.






                    share|improve this answer















                    This is the swift closure format:



                    {(parameter:type, parameter: type, ...) -> returntype in
                    //do stuff
                    }


                    This is what you should do:



                    //The animation closure will take no parameters and return void (nothing).
                    UIView.animateWithDuration(duration: NSTimeInterval, animations: {() -> Void in
                    //Animate anything.
                    })


                    Here is the documentation for closures.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jun 6 '15 at 20:01









                    luk2302

                    34.5k1773101




                    34.5k1773101










                    answered Jun 4 '14 at 13:27









                    67cherries67cherries

                    5,37763049




                    5,37763049























                        5














                        Following code can guide to write your own block.



                        class func testFunc(completion: ((list : NSArray!) -> Void)?) {
                        //--- block code.
                        if completion! != nil {
                        completion! (list: NSArray())
                        }
                        }


                        and you can call it like -



                        className.testFunc {
                        (list: NSArray!) -> Void in
                        }





                        share|improve this answer




























                          5














                          Following code can guide to write your own block.



                          class func testFunc(completion: ((list : NSArray!) -> Void)?) {
                          //--- block code.
                          if completion! != nil {
                          completion! (list: NSArray())
                          }
                          }


                          and you can call it like -



                          className.testFunc {
                          (list: NSArray!) -> Void in
                          }





                          share|improve this answer


























                            5












                            5








                            5







                            Following code can guide to write your own block.



                            class func testFunc(completion: ((list : NSArray!) -> Void)?) {
                            //--- block code.
                            if completion! != nil {
                            completion! (list: NSArray())
                            }
                            }


                            and you can call it like -



                            className.testFunc {
                            (list: NSArray!) -> Void in
                            }





                            share|improve this answer













                            Following code can guide to write your own block.



                            class func testFunc(completion: ((list : NSArray!) -> Void)?) {
                            //--- block code.
                            if completion! != nil {
                            completion! (list: NSArray())
                            }
                            }


                            and you can call it like -



                            className.testFunc {
                            (list: NSArray!) -> Void in
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Sep 30 '14 at 11:56









                            Prakash RajPrakash Raj

                            1,88611213




                            1,88611213























                                3














                                You can basically write it in 3 identical ways:



                                write what to do right in the closure/code block:



                                UIView.animateWithDuration(10.0) {
                                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                }


                                This is also known as trailing closure ( You can only do trailing closure if the closure parameter is the last parameter)



                                This doesn't mean the parameter 'animations' is no longer written. It is written but just as in the format of above.





                                Write exactly within the lines, most developers avoid such, because it's a little buggy to write with all the parenthesis and braces.



                                UIView.animateWithDuration(10.0, animations: {
                                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                })


                                (Contrary to trailing closure you wrote name ie 'animations')
                                This is known as inline closure





                                Write in a more modular sense



                                UIView.animateWithDuration(duration: NSTimeInterval, animations: animatingFunc)

                                func animatingFunc() {
                                self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                }


                                Remember the type of the parameter 'animations' was () -> Void



                                Exactly as what we are doing, animatingFunc takes no parameters ie '()' and returns nothing ie 'void'



                                (In Swift, functions are types and can be passed in as parameters)
                                Some might say this is more readable some might say trailing closure is...





                                Side note1
                                You can also do nothing ( which really doesn't make sense but in many other handlers/animations/completion handlers you may not want to do anything)



                                UIView.animateWithDuration(duration: NSTimeInterval, animations: nil)




                                Side note2



                                Closures becomes more interesting when you have to capture a value. See this simple demonstration.
                                For more information about Swift closures see Apple's Documentation






                                share|improve this answer






























                                  3














                                  You can basically write it in 3 identical ways:



                                  write what to do right in the closure/code block:



                                  UIView.animateWithDuration(10.0) {
                                  self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                  }


                                  This is also known as trailing closure ( You can only do trailing closure if the closure parameter is the last parameter)



                                  This doesn't mean the parameter 'animations' is no longer written. It is written but just as in the format of above.





                                  Write exactly within the lines, most developers avoid such, because it's a little buggy to write with all the parenthesis and braces.



                                  UIView.animateWithDuration(10.0, animations: {
                                  self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                  })


                                  (Contrary to trailing closure you wrote name ie 'animations')
                                  This is known as inline closure





                                  Write in a more modular sense



                                  UIView.animateWithDuration(duration: NSTimeInterval, animations: animatingFunc)

                                  func animatingFunc() {
                                  self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                  }


                                  Remember the type of the parameter 'animations' was () -> Void



                                  Exactly as what we are doing, animatingFunc takes no parameters ie '()' and returns nothing ie 'void'



                                  (In Swift, functions are types and can be passed in as parameters)
                                  Some might say this is more readable some might say trailing closure is...





                                  Side note1
                                  You can also do nothing ( which really doesn't make sense but in many other handlers/animations/completion handlers you may not want to do anything)



                                  UIView.animateWithDuration(duration: NSTimeInterval, animations: nil)




                                  Side note2



                                  Closures becomes more interesting when you have to capture a value. See this simple demonstration.
                                  For more information about Swift closures see Apple's Documentation






                                  share|improve this answer




























                                    3












                                    3








                                    3







                                    You can basically write it in 3 identical ways:



                                    write what to do right in the closure/code block:



                                    UIView.animateWithDuration(10.0) {
                                    self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                    }


                                    This is also known as trailing closure ( You can only do trailing closure if the closure parameter is the last parameter)



                                    This doesn't mean the parameter 'animations' is no longer written. It is written but just as in the format of above.





                                    Write exactly within the lines, most developers avoid such, because it's a little buggy to write with all the parenthesis and braces.



                                    UIView.animateWithDuration(10.0, animations: {
                                    self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                    })


                                    (Contrary to trailing closure you wrote name ie 'animations')
                                    This is known as inline closure





                                    Write in a more modular sense



                                    UIView.animateWithDuration(duration: NSTimeInterval, animations: animatingFunc)

                                    func animatingFunc() {
                                    self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                    }


                                    Remember the type of the parameter 'animations' was () -> Void



                                    Exactly as what we are doing, animatingFunc takes no parameters ie '()' and returns nothing ie 'void'



                                    (In Swift, functions are types and can be passed in as parameters)
                                    Some might say this is more readable some might say trailing closure is...





                                    Side note1
                                    You can also do nothing ( which really doesn't make sense but in many other handlers/animations/completion handlers you may not want to do anything)



                                    UIView.animateWithDuration(duration: NSTimeInterval, animations: nil)




                                    Side note2



                                    Closures becomes more interesting when you have to capture a value. See this simple demonstration.
                                    For more information about Swift closures see Apple's Documentation






                                    share|improve this answer















                                    You can basically write it in 3 identical ways:



                                    write what to do right in the closure/code block:



                                    UIView.animateWithDuration(10.0) {
                                    self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                    }


                                    This is also known as trailing closure ( You can only do trailing closure if the closure parameter is the last parameter)



                                    This doesn't mean the parameter 'animations' is no longer written. It is written but just as in the format of above.





                                    Write exactly within the lines, most developers avoid such, because it's a little buggy to write with all the parenthesis and braces.



                                    UIView.animateWithDuration(10.0, animations: {
                                    self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                    })


                                    (Contrary to trailing closure you wrote name ie 'animations')
                                    This is known as inline closure





                                    Write in a more modular sense



                                    UIView.animateWithDuration(duration: NSTimeInterval, animations: animatingFunc)

                                    func animatingFunc() {
                                    self.navigationController.toolbar.frame = CGRect(x:0.0, y:10.0, width:10.0, height:0.0)
                                    }


                                    Remember the type of the parameter 'animations' was () -> Void



                                    Exactly as what we are doing, animatingFunc takes no parameters ie '()' and returns nothing ie 'void'



                                    (In Swift, functions are types and can be passed in as parameters)
                                    Some might say this is more readable some might say trailing closure is...





                                    Side note1
                                    You can also do nothing ( which really doesn't make sense but in many other handlers/animations/completion handlers you may not want to do anything)



                                    UIView.animateWithDuration(duration: NSTimeInterval, animations: nil)




                                    Side note2



                                    Closures becomes more interesting when you have to capture a value. See this simple demonstration.
                                    For more information about Swift closures see Apple's Documentation







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jun 26 '18 at 21:15









                                    David

                                    2,3492742




                                    2,3492742










                                    answered Dec 1 '16 at 22:41









                                    HoneyHoney

                                    10.5k662124




                                    10.5k662124






























                                        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%2f24038713%2fsyntax-of-block-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

                                        MongoDB - Not Authorized To Execute Command

                                        in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                                        How to fix TextFormField cause rebuild widget in Flutter