2016-12-30 93 views
0

我有一個應用程序,可以從Core Data模型中獲取數據,然後我希望在地圖上顯示這些數據。當我向地圖視圖添加註釋時,出現以下錯誤:無法將類型'Swift._ContiguousArrayStorage <>'()的值轉換爲'MKAnnotation'()。這同樣的邏輯適用於Swift 2,但顯然不適用於Swift 3.我在哪裏以及如何將符號添加到地圖視圖。的代碼是:向地圖視圖添加註釋時出錯

override func viewDidAppear(_ animated: Bool) { 

    var rainFallGauges = [RainFallGauge]() 
    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "RainFallGauge") 

    do { 
     rainFallGauges = try coreDataStack.context.fetch(fetchRequest) as! [RainFallGauge] 

     obsCount = rainFallGauges.count 
    } catch let error as NSError { 
     print("Could not fetch \(error), \(error.userInfo)") 
    } 

    if obsCount == 0 { 

     let alert = UIAlertController(title: "Sorry", message: "No gauges found", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 
     self.present(alert, animated: true, completion: nil) 
    } else { 

     for rainFallGauge in rainFallGauges { 

      resultsGauge = rainFallGauge.value(forKey: "gaugeName") as? String 
      resultsLatitude = rainFallGauge.value(forKey: "gaugeLatitude") as? String 
      resultsLongitude = rainFallGauge.value(forKey: "gaugeLongitude") as? String 
      print(resultsGauge) 

      if (resultsLatitude != nil && resultsLongitude != nil) && (resultsLatitude?.isEmpty == false && resultsLongitude?.isEmpty == false) { 
       let latNSString = NSString(string: resultsLatitude!) 
       setLatitude = latNSString.doubleValue 
       print(setLatitude) 

       let longNSString = NSString(string: resultsLongitude!) 
       setLongitude = longNSString.doubleValue 
       print(setLongitude) 

       self.gauges.append(Gauges(gaugeTitle: resultsGauge, latitude: setLatitude, longitude: setLongitude)) 
       print(gauges.count) 
      } else if gauges.count == 0 { 
       let alert = UIAlertController(title: "Sorry", message: "No gauges", preferredStyle: UIAlertControllerStyle.alert) 
       alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 
       self.present(alert, animated: true, completion: nil) 
      } 
     } 
    } 

    self.gaugeMap.delegate = self 
    self.gaugeMap.addAnnotation(self.gauges as! MKAnnotation) 

    gaugeMap.mapType = .hybrid 

    let rectToDisplay = self.gauges.reduce(MKMapRectNull) { 
     (mapRect: MKMapRect, gauge: Gauges) -> MKMapRect in 
     let placePointRect = MKMapRect(origin: gauge.location.mapPoint, size: MKMapSize(width: 0, height: 0)) 

     return MKMapRectUnion(mapRect, placePointRect) 
    } 

我有數組「儀表」如上所聲明的:

var gauges = [Gauges]() 

和已創建的類儀表來保存註釋相關的信息。

任何建議,將引導我到addAnnotation語句的正確語法,它應該去的地方,將不勝感激。

+0

此行是沒有意義的:'self.gaugeMap.addAnnotation(

self.speciesMap.addAnnotations(self.places) 

我通過簡化和要解決我的問題self.gauges as!MKAnnotation)'如果'self.gauges'是一個'[Gauges]'它怎麼可能是一個MKAnnotation?不要告訴我在Swift 2中以某種方式工作;無論你在說什麼語言,都是無稽之談。 – matt

+0

是MKAnnotation的Gauges子類? –

+0

感謝您提供寶貴的見解,我誤解了我舊應用中的這一行: – user1698875

回答

0

謝謝你的見解。我誤讀了線在我的舊的應用程序,它實際上是:

for rainFallGauge in rainFallGauges { 

      resultsGauge = rainFallGauge.value(forKey: "gaugeName") as? String 
      resultsLatitude = rainFallGauge.value(forKey: "gaugeLatitude") as? String 
      resultsLongitude = rainFallGauge.value(forKey: "gaugeLongitude") as? String 
      print(resultsGauge) 

      if (resultsLatitude != nil && resultsLongitude != nil) && (resultsLatitude?.isEmpty == false && resultsLongitude?.isEmpty == false) { 
       let latNSString = NSString(string: resultsLatitude!) 
       setLatitude = latNSString.doubleValue 
       print(setLatitude) 

       let longNSString = NSString(string: resultsLongitude!) 
       setLongitude = longNSString.doubleValue 
       print(setLongitude) 

       gaugeMap.addAnnotation(OneGauge(title: resultsGauge, subtitle: "", coordinate: CLLocationCoordinate2D(latitude: setLatitude, longitude: setLongitude))) 

      } 
     } 
    } 
    gaugeMap.mapType = .hybrid