CLLocation longitude and latitude into variable

I want to create a property with CLLocation's longitude and latitude. I tried adding lazy before the variable. There is no error, but the longitude and latitude results are not applied to the attributes. I made a func and tried to use it but it's no use. It just throws an error saying it found nil. What is the problem? I tried to solve it by myself for a long time, but It is too difficult for me who started studying. I need your kind help. CLLocation in Swift please help.

Error

Cannot use instance member 'lat' within property initializer; property initializers run before 'self' is available

Problem is probably an async issue.

Geoloc func are async. So they return immediately and you have not yet the result when the next instruction is executed.

And please, tell where exactly you get the error message (which line).

1. import UIKit
2. import MapKit
3. import CoreLocation
4. 
5. class GetLocation: UIViewController, CLLocationManagerDelegate {
6.     
7. //    var isLocationEnabled = false
8.     
9.     var locationManager: CLLocationManager!
10.     
11.     var lat: Double = 0
12.     var lon: Double = 0
13.     
14.     var region = CLLocation(latitude: lat, longitude: lon)
15.   
16.     //    let geoCoder = CLGeocoder()
17.     
18.     override func viewDidLoad() {
19.         super.viewDidLoad()
20.         
21.         self.locationManager = CLLocationManager()
22.         self.locationManager?.delegate = self
23.        
24.     }
25.     
26.     func regionManager() {
27.         self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
28.         self.locationManager.requestAlwaysAuthorization()
29.         
30.         if CLLocationManager.locationServicesEnabled() {
31.             print("Location Enabled")
32.         } else {
33.             print("Location Not Enabled")
34.         }
35.     }
36.    
37.     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
38.         
39.         let location = locations[0] as CLLocation
40.         
41.         let latitube = location.coordinate.latitude
42.         let longitube = location.coordinate.longitude
43.         
44.         lat = latitube
45.         lon = longitube
46.     }
47. }

The good solution is probably to use async await pattern.

CLLocation longitude and latitude into variable
 
 
Q