因此,我正在尝试创建一个在 map 上带有自定义注释的应用程序,并且我一直在学习有关如何操作的教程。然而,当我输入这段代码时,它给了我错误
“mapView(_:viewFor:)”的重新声明无效
这是教程 - https://www.youtube.com/watch?v=w_aw72i8P_U
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard let _annotation = annotation as? MyAnnotation else {return }
let identifier = "marker"
var view: MKMarkerAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
}
else
{
view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
return view
}
}
最佳答案
这通常发生在您已经在别处实现该方法时。
检查您的 ViewController 以确保您没有 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {}
在该扩展之外实现。
关于ios - 'mapView(_:viewFor:)' 的重新声明无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58191363/