I was integrating my app with a map, and when I declare a variable span with MKCoordinateSpan
type, it gives me this error:
Cannot use instance member 'latDelta' within property initializer; property initializers run before 'self' is available
Also when I write other types such as: CLLocationCoordinate2D
or MKCoordinateRegion
it still says me that the variable within the property initializer cannot be used.
I don't know what the problem is, any help would be appreciated!
Here's my code:
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var map: MKMapView!
let latitude: CLLocationDegrees = 42.8
let longitude: CLLocationDegrees = 74.6
let latDelta: CLLocationDegrees = 0.05
let lonDelta: CLLocationDegrees = 0.05
let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
That is not correct way to initialise values. but you can do that in any other class method. Check below code:
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var map: MKMapView!
let latitude: CLLocationDegrees = 42.8
let longitude: CLLocationDegrees = 74.6
let latDelta: CLLocationDegrees = 0.05
let lonDelta: CLLocationDegrees = 0.05
//Declare it here
var span: MKCoordinateSpan?
override func viewDidLoad() {
super.viewDidLoad()
//Assign values here in method
span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)
}
}