Syntax of Block in Swift

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.


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)
}

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
}

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

Tags:

Ios

Swift

Swift3