Reputation: 11494
I am attempting to add location detection to an app and for the most part I have it figured out. But I am having issues with the reverseGeocodeLocation
method. I am getting the error cannot convert value of type 'CLLocation.Type' to expected argument type 'CLLocation'
. What would be causing this? Here is my code:
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
and location
are called above the viewDidLoad
:
var locationManager: CLLocationManager!
var location: String!
override func viewDidLoad() {
super.viewDidLoad()
loadLocation()
retrieveWeatherForecast()
}
If you find any other issues with my code you can point that out also.
After making the changes that trojanfoe suggested, this is what I have:
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 wants me to change var geoCoder: CLGeocoder!
to let geoCoder: CLGeocoder!
. If I do, I get this error Variable 'geoCoder' captured by a closure before being initialized
. What should I do?
Upvotes: 1
Views: 2855
Reputation: 122381
CLLocation
is the class (i.e. the type).
Don't you want to pass it one of the instances within the array that is passed to that method?
For example:
var location = locations.first
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(location: location) { ...
Upvotes: 2