在 ViewController 上获取初始化程序错误
Getting initializer error on ViewController
我收到错误:"Class 'ViewController' has no initializer"。我已将问题缩小到正在调用的这个函数。有人看到问题了吗?
let locationManager1: CLLocationManager // your location manager
func addBoundry(loc: CLLocation)
{
if let loc: CLLocation! = locationManager1.location {
let center: CLLocationCoordinate2D = loc!.coordinate
let lat: CLLocationDegrees = center.latitude
let long: CLLocationDegrees = center.longitude
var points = [CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long)]
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.addOverlay(polygon)
} else {
print("no location...")
}
}
当您说 let locationManager1: CLLocationManager
时,您是在声明 属性 不能 是 nil
,但实际上并没有将任何内容分配给它。
因此,您要么需要声明一个 init
以确保 locationManager1
恰好分配了一个值(毕竟它是一个 let
常量,而不是 var
!) 或像这样初始化 属性 内联:
let locationManager1 = CLLocationManager()
分配 属性 时使用可选。
let locationManager1: CLLocationManager?
我收到错误:"Class 'ViewController' has no initializer"。我已将问题缩小到正在调用的这个函数。有人看到问题了吗?
let locationManager1: CLLocationManager // your location manager
func addBoundry(loc: CLLocation)
{
if let loc: CLLocation! = locationManager1.location {
let center: CLLocationCoordinate2D = loc!.coordinate
let lat: CLLocationDegrees = center.latitude
let long: CLLocationDegrees = center.longitude
var points = [CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long)]
let polygon = MKPolygon(coordinates: &points, count: points.count)
mapView.addOverlay(polygon)
} else {
print("no location...")
}
}
当您说 let locationManager1: CLLocationManager
时,您是在声明 属性 不能 是 nil
,但实际上并没有将任何内容分配给它。
因此,您要么需要声明一个 init
以确保 locationManager1
恰好分配了一个值(毕竟它是一个 let
常量,而不是 var
!) 或像这样初始化 属性 内联:
let locationManager1 = CLLocationManager()
分配 属性 时使用可选。
let locationManager1: CLLocationManager?