Greg
Greg

Reputation: 34838

why in this code would I be getting "Unexpected non-void return value in void function"

Here is the code below:

private func getReverseGeocodeData(newCoordinates : CLLocationCoordinate2D) -> CLPlacemark? {
  let clLocation = CLLocation(latitude: newCoordinates.latitude, longitude: newCoordinates.longitude)
  GCAnnotation.geocoder.reverseGeocodeLocation(clLocation) { placemarks, error in
    if let pms = placemarks {
      let pm : CLPlacemark? = pms.first as CLPlacemark?
      return pm // ==> "Unexpected non-void return value in void function"
    }
  }
  return nil
}

Upvotes: 0

Views: 149

Answers (2)

Christian Abella
Christian Abella

Reputation: 5797

You need to add a callback parameter in your function that you can call after the reverseGeocodeLocation is finished and pass the pm as parameter.

private func getReverseGeocodeData(callback : (CLPlaceMark?)-> Void, newCoordinates : CLLocationCoordinate2D) -> CLPlacemark? {
  let clLocation = CLLocation(latitude: newCoordinates.latitude, longitude: newCoordinates.longitude)
  GCAnnotation.geocoder.reverseGeocodeLocation(clLocation) { placemarks, error in
    if let pms = placemarks {
      let pm : CLPlacemark? = pms.first as CLPlacemark?
       callback(pm)
    }
  }
  return nil
}

Upvotes: 1

Ted Huinink
Ted Huinink

Reputation: 856

GCAnnotation.geocoder.reverseGeocodeLocation(clLocation) is in it's own closure and function. When you use a callback like that you can't return a value like that. However if you are sure that that function returns a value immediately you could the following:

private func getReverseGeocodeData(newCoordinates : CLLocationCoordinate2D) -> CLPlacemark? {
    let pm: CLPlacemark?
    let clLocation = CLLocation(latitude: newCoordinates.latitude, longitude: newCoordinates.longitude)
    GCAnnotation.geocoder.reverseGeocodeLocation(clLocation) { placemarks, error in
        if let pms = placemarks {
             pm = pms.first as CLPlacemark?
        }
    }
    return pm
}

Upvotes: 1

Related Questions