我有一个应用程序,我从中获取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)
}
我将数组“gauges”声明为:
var gauges = [Gauges]()
并创建了Class Gauges来保存注释的相关信息。
任何可以指导我使用addAnnotation语句的正确语法以及应该去哪里的建议都将不胜感激。
答案 0 :(得分:0)
感谢您的见解。我在我的旧应用程序中误读了这一行,实际上是:
self.speciesMap.addAnnotations(self.places)
我通过简化和解决问题解决了我的问题:
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