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 trialjames bunn
2,377 PointsHow is "completion: (CurrentWeather? -> Void)" a closure?
How is "completion: (CurrentWeather? -> Void )" a closure?
In the book, apple says closures take one of three forms:
- Global functions are closures that have a name and do not capture any values
- Nested functions are closures that have a name and can capture values from their enclosing function
- 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
21,046 PointsHey 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