Trailing Closures

When you write a closure as a parameter of a function, the closure is often the last parameter. You see this often in Objective-C as well with blocks. In Objective-C, you write something like this when animating:

 [UIView animateWithDuration:1.50 delay:0options:(UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState)animations:^{    //do animations here.}];

Notice that this function takes two blocks, and you have one block at the end. You could write the last block as a trailing closure. This means you could write the closure outside the function’s closing parentheses. You could rewrite the last function in Swift like so:

UIView.animateWithDuration(1.50) ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.