我将我的应用程序与地图集成在一起,当我用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)发布于 2017-12-26 08:40:51
这不是初始化值的正确方式。但在任何其他类方法中都可以这样做。请检查以下代码:
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)
}
}发布于 2017-12-26 08:39:50
您需要在viewDidLoad()中初始化以下内容
let span: MKCoordinateSpan?; 在viewDidLoad()中
span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta);https://stackoverflow.com/questions/47975922
复制相似问题