我有一个应用程序,可以在其中从核心数据模型中获取数据,然后将其显示在地图上。当我向地图视图添加注释时,出现错误:无法将类型'Swift._ContiguousArrayStorage '()()转换为'MKAnnotation'()。相同的逻辑适用于Swift 2,但显然不适用于Swift3。我在哪里以及如何在地图视图中添加符号。代码是: 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语句的语法以及应将其放在何处。 最佳答案 感谢您的见解。我在旧应用中误读了该行,实际上是: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关于swift - 向 map View 添加注释时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41389907/
10-14 23:28