MKCoordinateSpan 中的错误,在自我可用之前说到 运行

Error in MKCoordinateSpan, says to run before the self is available

我正在将我的应用程序与地图集成,当我声明一个类型为 MKCoordinateSpan 的变量跨度时,它给了我这个错误:

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

此外,当我写其他类型时,例如:CLLocationCoordinate2DMKCoordinateRegion 它仍然告诉我不能使用 属性 初始值设定项中的变量。

我不知道问题是什么,任何帮助将不胜感激!

这是我的代码:

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)

您需要在 viewDidLoad()

中初始化以下内容
let span: MKCoordinateSpan?; 

并且在viewDidLoad()

span  = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta);

这不是初始化值的正确方法。但您可以使用任何其他 class 方法来做到这一点。检查以下代码:

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)
    }

}