无效的“ mapView(_:viewFor :)”声明

时间:2019-10-01 19:46:32

标签: ios swift xcode

因此,我正在尝试在地图上创建带有自定义注释的应用,并且我一直在关注如何操作的教程。但是,当我把这段代码放进去时,它给了我错误

'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
    }
}

1 个答案:

答案 0 :(得分:2)

通常在您已在其他地方实现该方法时发生。

检查您的ViewController,以确保您未在​​该扩展程序之外实施func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {}