2016-08-09 20 views
0

我得到的錯誤:「類'ViewController'沒有初始化程序」。我已經將問題縮小到了被調用的這個函數。有沒有人看到這個問題?在ViewController上獲取初始化器錯誤

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...") 
     } 
    } 
+0

檢查[這些搜索結果](http://stackoverflow.com/search?q=%5Bswift%5D+class+has+no+initializer)。 – rmaddy

回答

1

時指定屬性使用可選。

let locationManager1: CLLocationManager? 
1

當你說let locationManager1: CLLocationManager,你聲明一個屬性,不得nil,但實際上沒有任何分配給它。

所以,你要麼需要申報,確保locationManager1init被分配一個值(這是一個let常量,不是一個var,畢竟!)或初始化屬性內聯,如:

let locationManager1 = CLLocationManager()