我正在尝试将位置检测添加到应用程序,并且在大多数情况下我已经弄清楚了。但是我遇到了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
。我该怎么办?
答案 0 :(得分:2)
CLLocation
是类(即类型)。
你不想传递给传递给该方法的数组中的一个实例吗?
例如:
var location = locations.first
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location: location) { ...