gpt4 book ai didi

ios - 是什么导致错误 "cannot convert value of type ' CLLocation.Type' 到预期参数类型 'CLLocation'“

转载 作者:搜寻专家 更新时间:2023-11-01 07:29:47 24 4
gpt4 key购买 nike

我正在尝试将位置检测添加到应用程序中,并且我已经弄明白了大部分内容。但是我在使用 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
}
}

locationManagerlocationviewDidLoad 之上调用:

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。我该怎么办?

最佳答案

CLLocation 是类(即类型)。

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

例如:

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

关于ios - 是什么导致错误 "cannot convert value of type ' CLLocation.Type' 到预期参数类型 'CLLocation'“,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33349702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com