Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Swift Closures Closures in iOS Why Are Closures Useful?

Marcos Reboucas
Marcos Reboucas
12,890 Points

Can someone help me fully understand this example?

Just to clarify the usage concept in this animation example. The animateWithDuration function accepts a closure parameter as a callback:

 animations: { () -> Void in
    someLabel.alpha = 0.3
    }

That's ok I got it.

But what's the second closure? Is it a parameter? For what function?

{ (failure: Bool) -> Void in}

1 Answer

Jhoan Arango
Jhoan Arango
14,575 Points

Hello:

What you are looking at is a "class method".

class func animateWithDuration(_ duration: NSTimeInterval,
                    animations animations: () -> Void,
                    completion completion: ((Bool) -> Void)?) 

This class method has 3 parameters which takes block objects (closures) as it's arguments. The first parameter is not a closure, but the subsequent parameters are. The completion parameter takes a block object to be executed when the animation sequence ends. This block has no return value and takes a single Boolean argument that indicates whether or not the animations actually finished before the completion handler was called.

Hope this helps