1
votes

I upgraded to a newer Swift compiler and ran into this compiler error I can’t figure out how to resolve. I have a bunch of mapView functions declared from the MKMapViewDelegate. They all seem to match up except this one which throws this error:

ViewController.swift:137:10: Objective-C method 'mapView:viewForAnnotation:' provided by method 'mapView(:viewForAnnotation:)' conflicts with optional requirement method 'mapView(:viewForAnnotation:)' in protocol ‘MKMapViewDelegate’

ViewController.swift:12:7: Class 'ViewController' declares conformance to protocol 'MKMapViewDelegate’ here

/ViewController.swift:137:10: Requirement 'mapView(_:viewForAnnotation:)' declared here (MapKit.MKMapViewDelegate)

I looked at MKMapViewDelegate method and I thought I was matching it up correctly so I am baffled as to what has changed that needs correction. From there I see

optional func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView!

Here’s my declaration below that throws the error.

    class ViewController: UIViewController, MKMapViewDelegate {

        func mapView(aMapView: MKMapView!, viewForAnnotation annotation: CustomMapPinAnnotation!) -> MKAnnotationView! {
         //The error is thrown here on the m in mapView
        }
    }
1

1 Answers

2
votes

I looked at MKMapViewDelegate method and I thought I was matching it up correctly so I am baffled as to what has changed that needs correction. From there I see

optional func mapView(mapView: MKMapView!, 
    viewForAnnotation annotation: MKAnnotation!) 
    -> MKAnnotationView!

Really? You think your declaration matches that? Look closer. You have:

func mapView(aMapView: MKMapView!, 
    viewForAnnotation annotation: CustomMapPinAnnotation!) 
    -> MKAnnotationView! {

Rewrite it like this:

func mapView(aMapView: MKMapView!, 
    viewForAnnotation annotation: MKAnnotation!)
    -> MKAnnotationView! {

Do you see the difference? It is not your place to alter the types in the delegate method declaration. The declaration has MKAnnotation, you must declare MKAnnotation. This MKAnnotation may or not also be a CustomMapPinAnnotation, but that is for your code inside the body of the function to decide.