Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Closures Closures in iOS Using a Closure

james bunn
james bunn
2,377 Points

If the completion parameter is a closure, why doesn't it implement brackets around the parameters

From the earlier courses, when defining a closure, we always used brackets around the parameters. If the "completion" parameter is a closure, why doesn't it implement brackets

e.i { (parameters) -> return type in Statements }

For example. completion: { ([String:AnyObject]?) -> ()) }

james bunn
james bunn
2,377 Points

Thanks Deutsch, I reviewed the additional documents and unfortunately, I'm still at a loss for understanding how a completion handler qualifies as a closure. The "Swift Closures and Functions" doesn't explain why we don't need the curly braces when specifying a completion handler. The "Completion Handlers" document briefly mentions the use of a completion handler, but doesn't elaborate on why a completion handler qualifies as a closure.

I may be a bit hard headed on this, but just looking for a clean explanation on how the completion handler qualifies as a closure and why the syntax doesn't require a curly brace.

1 Answer

james bunn
james bunn
2,377 Points

Thanks Deutsch. I read this section multiple times and I'm still having difficulty. Is there a particular section of the Closure chapter you'd recommend?

Steven Deutsch
Steven Deutsch
21,046 Points

Everything up to Closures are Reference Types. This doesn't mean the entire chapter isn't important though. Here's some more reading if you want.

  1. Swift Closures and Functions
  2. Completion Handlers