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!

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 Build a Weather App with Swift Managing Complexity ForecastService

james bunn
james bunn
2,377 Points

How is "completion: (CurrentWeather? -> Void)" a closure?

How is "completion: (CurrentWeather? -> Void )" a closure?

In the book, apple says closures take one of three forms:

  1. Global functions are closures that have a name and do not capture any values
  2. Nested functions are closures that have a name and can capture values from their enclosing function
  3. Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context

May I confirm which of the three forms the example closure above relates to?

Thanks so much!

1 Answer

Steven Deutsch
Steven Deutsch
21,046 Points

Hey James Bunn,

Completion is a parameter of a function. This parameter takes a closure with the signature (CurrentWeather? -> Void). The closure expression would be written right inside of the parentheses after the function's local parameter name, much like writing a default value. If the closure is the last argument of the function, you can write it as a trailing closure, by pulling it outside of the parenthesis and writing it directly afterwards inside of curly braces.

Good Luck