Search code examples
iosgeocodeswift2

CLGeocoder Swift 2 Version


This used to work fine in Swift 1.2 but now gives the error:

"Cannot invoke 'geocodeAddressString' with an argument list of type '(String, completionHandler: ([AnyObject]!, NSError!) -> Void)'"

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [AnyObject]!, error: NSError!) -> Void in
            if let placemark = placemarks?[0] as? CLPlacemark {
                let annotation = MKPointAnnotation()

EDIT ANSWER:

geocoder.geocodeAddressString(address, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

Solution

  • the variables in completion handler are not setup correctly, you don't include the declarations so just -

    coder.geocodeAddressString("1 infinite loop, cupertino, ca") { (placemarks, error) -> Void in
    
        if let firstPlacemark = placemarks?[0] {
            print(firstPlacemark)
        }   
    }
    

    Note that optional cast not necessary as type inference knows will be CLPlacemark