MKCoordinateSpan中的错误,表示在自我可用之前运行

问题描述 投票:1回答:2

我正在将我的应用程序与地图集成,当我使用MKCoordinateSpan类型声明变量span时,它会给我这个错误:

不能在属性初始值设定项中使用实例成员'latDelta';属性初始化程序在'self'可用之前运行

此外,当我写其他类型,如: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)
ios swift mkmapview core-location
2个回答
-1
投票

这不是初始化值的正确方法。但你可以在任何其他类方法中做到这一点。检查以下代码:

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

}

0
投票

您需要在viewDidLoad()中初始化以下内容

let span: MKCoordinateSpan?; 

viewDidLoad()

span  = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta);
© www.soinside.com 2019 - 2024. All rights reserved.