【问题标题】:What is causing the error "cannot convert value of type 'CLLocation.Type' to expected argument type 'CLLocation'"是什么导致错误“无法将'CLLocation.Type'类型的值转换为预期的参数类型'CLLocation'”
【发布时间】:2016-01-25 18:51:28
【问题描述】:

我正在尝试将位置检测添加到应用程序中,并且在大多数情况下我已经弄清楚了。但是我遇到了reverseGeocodeLocation 方法的问题。我收到错误cannot convert value of type 'CLLocation.Type' to expected argument type 'CLLocation'。什么会导致这种情况?这是我的代码:

func loadLocation() {
    self.locationManager = CLLocationManager()
    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = 2000
    self.locationManager.startUpdatingLocation()
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    self.locationManager.stopUpdatingLocation()
    var location = locations.first
    var geoCoder: CLGeocoder!

    geoCoder.reverseGeocodeLocation(location: CLLocation) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in
                                                    ^ Error Here
        var placemark: CLPlacemark = placemarks?.first
        self.location = placemark.locality
        self.location = placemark.administrativeArea
    }
}

locationManager 和 location 在 viewDidLoad 之上调用:

var locationManager: CLLocationManager!
var location: String!

override func viewDidLoad() {
    super.viewDidLoad()

    loadLocation()
    retrieveWeatherForecast()
}

如果您发现我的代码有任何其他问题,您也可以指出来。

更新:

在进行了 trojanfoe 建议的更改后,这就是我所拥有的:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    self.locationManager.stopUpdatingLocation()
    let location = locations.first
    var geoCoder: CLGeocoder!

    geoCoder.reverseGeocodeLocation(location!) { (placemarks: [CLPlacemark]?, error: NSError?) -> Void in

        let placemark: CLPlacemark = (placemarks?.first)!
        self.location = placemark.locality
        self.location = placemark.administrativeArea
    }
}

Xcode 想让我把 var geoCoder: CLGeocoder! 改成 let geoCoder: CLGeocoder!。如果我这样做,我会收到此错误Variable 'geoCoder' captured by a closure before being initialized。我该怎么办?

【问题讨论】:

    标签: ios swift core-location cllocation reverse-geocoding


    【解决方案1】:

    CLLocation 是类(即类型)。

    您不想将数组中传递给该方法的实例之一传递给它吗?

    例如:

    var location = locations.first
    let geoCoder = CLGeocoder()
    geoCoder.reverseGeocodeLocation(location: location) { ...
    

    【讨论】:

    • @CalebKleveter 是的,您需要在使用之前初始化geoCoder。
    • 在 Swift 中是如何做到的?我知道如何在 Objective-C 中做到这一点,但不知道 Swift。
    • @CalebKleveter 更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    • 2023-04-04
    • 2016-05-03
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多