我正在将我的应用程序与地图集成,当我使用MKCoordinateSpan
类型声明变量span时,它会给我这个错误:
不能在属性初始值设定项中使用实例成员'latDelta';属性初始化程序在'self'可用之前运行
此外,当我写其他类型,如:CLLocationCoordinate2D
或MKCoordinateRegion
时,它仍然告诉我,属性初始值设定项中的变量不能使用。
我不知道问题是什么,任何帮助将不胜感激!
这是我的代码:
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)
这不是初始化值的正确方法。但你可以在任何其他类方法中做到这一点。检查以下代码:
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)
}
}
您需要在viewDidLoad()
中初始化以下内容
let span: MKCoordinateSpan?;
在viewDidLoad()
span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta);