JoeW467 JoeW467 - 4 months ago 19
iOS Question

What does this code in Swift do?

I'm following a tutorial on using the Google Maps iOS SDK. I came across this piece of code which is halfway down this link here:

I am familiar with Swift but I do not get what the piece of code after geocoder.reverseGeocodeCoordinate(coordinate) does; specifically, how can you just place the curly brackets right after the method call and what does it accomplish? I am asking this in terms of Swift syntax.

func reverseGeocodeCoordinate(coordinate: CLLocationCoordinate2D) {

// 1
let geocoder = GMSGeocoder()

// 2
geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
if let address = response?.firstResult() {

// 3
let lines = address.lines as! [String]
self.addressLabel.text = lines.joinWithSeparator("\n")

// 4
UIView.animateWithDuration(0.25) {


This is called a "trailing closure" or "trailing closure syntax". It's described in Apple's docs here:

In summary, if the last parameter of a method or function is a closure, you can provide it immediately after the closing ) of the function call and the previous arguments. Example:

func sendMessage(text:String, withCallback:(Bool)->()) {
    // Implementation

let message = "hello"
sendMessage(message) { 
    let result = $0 ? "Suceeded" : "Failed"

If a function take only one parameter and that parameter is a closure, then you don't need to use () at all and can just pass the closure immediately after the function name. For example see how this filter method is called:

let boolArray = [true, true, false, true, false]
let filtered = boolArray.filter { $0 == false }  // Only the falses