0

I am a bit struggling to construct a call in SWIFT for this function

func addressFromLocation(location:CLLocation!, completionClosure:((NSDictionary?)->())){

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { () -> Void in
        var geoCoder = CLGeocoder()
        geoCoder.reverseGeocodeLocation(location, completionHandler: { (placeMarks, error) -> Void in
            if let places = placeMarks {
                var marks = places[0] as! CLPlacemark
                completionClosure(marks.addressDictionary)
            }else {
                completionClosure(nil)
            }

        })
    })
}

Can anybody help?

Vanya
  • 4,973
  • 5
  • 32
  • 57

1 Answers1

0

First, you can remove unnecessary parentheses from completionClosure parameter type:

func addressFromLocation(location: CLLocation!, completionClosure: NSDictionary? -> ()){
    //...
}

And you can call this function like so:

addressFromLocation(location, completionClosure: {
    dict in
    // your code
})

Or even simpler because if closure is final argument of function then you can use trailing closure syntax:

addressFromLocation(location) {
    dict in
    // your code
}

And if you are do not need named parameter dict then you can refer parameter by number:

addressFromLocation(location) {
    print($0)
}
mixel
  • 25,177
  • 13
  • 126
  • 165