Everytime I ask a question here, it's because I don't know what to search for, and I'm very sorry for that.
Anyway, I'm trying to create an application using Google Maps SDK, and I followed a very nice tutorial by Ron Kliffer on Ray Wenderlich's site. However, I want to customize it pretty heavy, so I'm trying to read every line and understanding what it does. Now I ran into a block, where I do not understand the syntax.
func fetchPlacesNearCoordinate(coordinate: CLLocationCoordinate2D, radius:
Double, types:[String], completion: (([GooglePlace]) -> Void)) -> ()
That's the function that's being called, this way:
dataProvider.fetchPlacesNearCoordinate(coordinate, radius:mapRadius, types: searchedTypes) { places in
for place: GooglePlace in places {
I simply don't understand the completion: bit, and the "places in" bit. What follows is, if I'm not wrong, a "foreach" thing (or for in in Swift syntax.
EDIT: reformat
Edit2: And yes, an array of GooglePlaces is created in the function body, and the same array (I guess) is used in the forin-thing.
{ places in
for place: GooglePlace in places {
// ...
}
}
is a closure. The general closure expression syntax is
{ (param_1 : type_1, ..., param_n : type_n ) -> return_type in
statements
}
When the closure type is known or can be inferred from the context then you can omit the parameter types, the return type (and also the parentheses around the parameters):
{ param_1, ..., param_n in
statements
}
So the first places
is the (only) closure parameter, and it is used in the for-in statement.
Note that since the closure has the type ([GooglePlace]) -> Void
, the places
parameter has the type [GooglePlace]
and the explicit type annotation in the for-in loop is not necessary:
{ places in
for place in places {
// ...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.