user1437787
user1437787

Reputation: 7

Swift 2 reverseGeocodeLocation error

I'm new to Swift development. I just converted existing working code to swift2 while updating Xcode 7 from 6.

    private func decodeLocation(array: [AnyObject]!, err: NSError!) -> Void {
    if err == nil {
        let mark = array.first as! CLPlacemark
        country = mark.ISOcountryCode
        city = mark.subAdministrativeArea!.stringByReplacingOccurrencesOfString(" ", withString: "", options: NSStringCompareOptions(), range: nil)
        region = mark.administrativeArea
        coordinates = "\(mark.location!.coordinate.latitude),\(mark.location!.coordinate.longitude)"
        print("Country: \(country!)")
        print("City: \(city!)")
        print("Region: \(region!)")
        print("Loc: \(coordinates!)")
        delegate?.didGetLocation("\(coordinates!)")
    }
}
// Delegates
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == CLAuthorizationStatus.AuthorizedAlways || status == CLAuthorizationStatus.AuthorizedWhenInUse {
        locationManager.startUpdatingLocation()
    }
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    //An array of CLLocation objects. The most recent location update is at the end of the array.

    dispatch_once(&geoCoderToken, {
        self.currentLocation = locations.last as? CLLocation!
        self.geoCoder.reverseGeocodeLocation(self.currentLocation!, completionHandler: self.decodeLocation)
    })

}

When I build, I get this error.

Cannot convert value of type '([AnyObject]!, err: NSError!) -> Void' to expected argument type 'CLGeocodeCompletionHandler' (aka '(Optional<Array<CLPlacemark>>, Optional<NSError>) -> ()')

Please let me know if you have any idea. Thanks!

Upvotes: 0

Views: 862

Answers (1)

Justin Levi Winter
Justin Levi Winter

Reputation: 2737

I think the signature on your completion handler is incorrect

self.decodeLocation

The method needs to look like this:

func decodeLocation(array:[CLPlacemark]?, error:NSError?) {
...
}

Update:

your callback function still doesn't seem to have the signature that the compiler wants.

decodeLocation(array: [AnyObject]!, err: NSError!) -> Void

does not equal

``` (Optional>, Optional) -> ()

```

This is the longhand version of the shorthand syntax I posted above.

func decodeLocation(array:Array<CLPlacemark>?, error:<NSError>?) {
...
}

If you have a playground or a project on github I can look at I might be able to help more.

Upvotes: 1

Related Questions