Swifty
Swifty

Reputation: 37

CLLocationManager no value comes out of the func

I need a value for the "city" property, but it prints nil, how can I get a value?

I tried adding an output parameter -> String to the func but it gives me many errors

class ViewController: UIViewController {
var city: String?
}
    
extension ViewController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
                
guard let first = locations.first else {
return
}
                
let clLocation = CLLocation(latitude: first.coordinate.latitude, longitude: first.coordinate.longitude)
                
ViewController.geoCoder.reverseGeocodeLocation(clLocation) { placemarks, _ in
                    
if let place = placemarks?.first {
self.city = place.locality
print("CITY: \(String(describing: city))") // YES PRINT CITY
}
                    
}
                
}
    
print("CITY: \(String(describing: city))") // NO PRINT CITY, print nil
    
}

Upvotes: 0

Views: 27

Answers (0)

Related Questions