导致错误的原因 "cannot convert value of type 'CLLocation.Type' to expected argument type 'CLLocation'"
What is causing the error "cannot convert value of type 'CLLocation.Type' to expected argument type 'CLLocation'"
我正在尝试将位置检测添加到应用程序中,并且大部分我都已经弄清楚了。但是我对 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
。我该怎么办?
CLLocation
是 class(即 类型 )。
您不想将传递给该方法的数组中的实例之一传递给它吗?
例如:
var location = locations.first
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location: location) { ...
我正在尝试将位置检测添加到应用程序中,并且大部分我都已经弄清楚了。但是我对 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
。我该怎么办?
CLLocation
是 class(即 类型 )。
您不想将传递给该方法的数组中的实例之一传递给它吗?
例如:
var location = locations.first
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location: location) { ...