Ios 如何在swift中返回CLLocation值?

Ios 如何在swift中返回CLLocation值?,ios,swift,cllocation,Ios,Swift,Cllocation,我正在尝试创建可以返回CLLocation值的函数 由于这是一个异步方法,它会在一段时间后给出值,并在该时间之前返回get called。朗,拿到lat后有没有办法回来 func setLocation(location: String) -> [String]{ var lat: CLLocationDegrees? var lon: CLLocationDegrees? geocoder.geocodeAddres

我正在尝试创建可以返回CLLocation值的函数

由于这是一个异步方法,它会在一段时间后给出值,并在该时间之前返回get called。朗,拿到lat后有没有办法回来

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) 
            }
}


如果可能的话,我可能不知道该怎么做。你能提供代码片段吗?@Sh_khann提供谢谢。由于我是新手,我可以知道如何使用它吗?请参见编辑。。。。。。。。。。。。。。。。。。。。。。。。。。。。
setLocation(location: "") { (coor) in
   print(coor.latitude)
}