2

I used the function geocodeAddressString which worked in swift 1.0, but not in swift2. Can anybody tell me what's wrong with my code and how i can fix this? Thank you!

geocoder.geocodeAddressString(address, {(placemarks: [AnyObject], error: NSError) -> Void in  //Error: Missing argument for parameter 'completionHandler' in call

        if let placemark = placemarks?[0] as? CLPlacemark {
            let annotation = MKPointAnnotation()
            let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
            annotation.coordinate = location
            annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
            annotation.subtitle = "\(StudentArray[student].grade)"
            self.mapView.addAnnotation(annotation)
        }

    })
Jerry Xu
  • 295
  • 2
  • 19

2 Answers2

2

Either specify the completionHandler parameter name:

geocoder.geocodeAddressString(address, completionHandler: { placemarks, error in
    if let placemark = placemarks.first as? CLPlacemark {
        let annotation = MKPointAnnotation()
        let location = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
        annotation.coordinate = location
        annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
        annotation.subtitle = "\(StudentArray[student].grade)"
        self.mapView.addAnnotation(annotation)
    }
})

Or use trailing closure syntax (see Trailing Closure section of Closures chapter of The Swift Programming Language), where you pull the closure out from within the ( and ) and put it after the ):

geocoder.geocodeAddressString(address) { placemarks, error in
    if let placemark = placemarks.first as? CLPlacemark {
        let annotation = MKPointAnnotation()
        let location = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
        annotation.coordinate = location
        annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
        annotation.subtitle = "\(StudentArray[student].grade)"
        self.mapView.addAnnotation(annotation)
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks for providing this example! The trailing closure syntax always confused me since it omitted a part of the parameter, but now I see it is just short hand practice. – daspianist Mar 27 '17 at 03:05
1

Add in the argument before the completion handler.

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [AnyObject], error: NSError) -> Void in  // Added argument for parameter 'completionHandler' in call

    if let placemark = placemarks?[0] as? CLPlacemark {
        let annotation = MKPointAnnotation()
        let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude)
        annotation.coordinate = location
        annotation.title = "\(StudentArray[student].firstName), \(StudentArray[student].lastName)"
        annotation.subtitle = "\(StudentArray[student].grade)"
        self.mapView.addAnnotation(annotation)
    }

})
Swinny89
  • 7,273
  • 3
  • 32
  • 52