我怎样才能 return 在 swift 中的 CLLocation 值?

How can I return a CLLocation value in swift?

我正在尝试创建可以 return CLLocation 值的函数?

因为这是异步方法,它会在一段时间后给出值,return 在此之前被调用。有没有办法在得到经纬度后return?

func setLocation(location: String) -> [String]{
            var lat: CLLocationDegrees?
            var lon: CLLocationDegrees?
            geocoder.geocodeAddressString(location) { placemarks, error in
                let placemark = placemarks?.first
                lat = placemark?.location?.coordinate.latitude
                lon = placemark?.location?.coordinate.longitude
                print("Lat: \(lat), Lon: \(lon)") //here i get value
            }

            print("Lat: \(lat), Lon: \(lon)") //but here i get nil

            return ["\(String(describing: lat))", "\(String(describing: lon))"]

        }

使用补全

func setLocation(location: String,completion:@escaping(CLLocationCoordinate2D? -> ())) {
            var lat: CLLocationDegrees?
            var lon: CLLocationDegrees?
            geocoder.geocodeAddressString(location) { placemarks, error in
                let placemark = placemarks?.first
                completion(placemark?.location?.coordinate) 
            }
}

setLocation(location: "") { (coor) in
   print(coor.latitude)
}