如何 return 来自 CLGeocoder 的值?

How to return a value from CLGeocoder?

我正在开发天气应用作为培训,需要将位置转换为城市。所以我像这样使用 CLGeocoder:

 func updateWeatherData(json: JSON?) {

        if let json = json {
            weatherData.temperature = fahrenheitToCelcius(json["currently"]["temperature"].doubleValue)
            weatherData.weatherIconName = json["currently"]["icon"].stringValue

            let location = CLLocation(latitude: json["latitude"].doubleValue, longitude: json["longitude"].doubleValue)
            CLGeocoder().reverseGeocodeLocation(location) { (placemark, error) in

                if let city = placemark {
                    self.weatherData.city = city.last?.locality
                } else if let error = error {
                    print(error)
                }
            }
            updateUIWithWeatherData()
        }

    }
    //MARK: - UI Updates

    func updateUIWithWeatherData() {

        cityLabel.text = weatherData.city
        temperatureLabel.text = "\(weatherData.temperature)°"
        weatherIcon.image = UIImage(named: weatherData.weatherIconName!)

    }

而此代码 returns 为 weatherData.city。但是当我在闭包中放置一个断点时,一切正常。我错过了什么?

如果我对您的问题的理解正确,您需要在地理编码完成后更新您的 UI。像下面这样:

func updateWeatherData(json: JSON?) {

    if let json = json {
        weatherData.temperature = fahrenheitToCelcius(json["currently"]["temperature"].doubleValue)
        weatherData.weatherIconName = json["currently"]["icon"].stringValue

        let location = CLLocation(latitude: json["latitude"].doubleValue, longitude: json["longitude"].doubleValue)
        CLGeocoder().reverseGeocodeLocation(location) { (placemark, error) in

            if let city = placemark {
                self.weatherData.city = city.last?.locality
            } else if let error = error {
                print(error)
            }
            self.updateUIWithWeatherData()
        }            
    }

}

这些操作的顺序是地理编码是异步完成的,并且可能会在代码调用之后发生。注意可以但不需要。

您还应该阅读有关线程的此方法的文档。 UI 必须在主线程上更新,因此除非文档指定调用将在主线程上完成,否则您最好强制执行它:

CLGeocoder().reverseGeocodeLocation(location) { (placemark, error) in    
    if let city = placemark {
        self.weatherData.city = city.last?.locality
    } else if let error = error {
        print(error)
    }
    DispatchQueue.main.async {
        self.updateUIWithWeatherData()
    }
}