1

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
        }
    }
Kokanee
  • 975
  • 4
  • 9
  • 20

1 Answers1

2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Ok then... what is intetresting is the compiler just ignored this previously and it just worked. I suppose as the language tweaks were put to tighten up areas for further type enforcement. My CustomMapPinAnnotation declaration was: – Kokanee May 06 '15 at 16:05
  • I understand that it just worked previously. But this, as you rightly say, was because the compiler was letting it slide - and now it doesn't. – matt May 06 '15 at 16:08
  • finishing my comment from above just for reference to others, it's all back and working now. class CustomMapPinAnnotation : NSObject, MKAnnotation { } and now I just matched it 100% and declared a var as: var myannotation = annotation as? CustomMapPinAnnotation – Kokanee May 06 '15 at 16:20
  • Correct-a-mundo, well done! And this is much more correct code anyway, because now you are guarding against the possibility that this might _not_ be a CustomMapPinAnnotation. – matt May 06 '15 at 16:34
  • I also had some code up top in mapView to check if my custom class was incoming and to bail out of not... was advised on this many moons ago in the beta days... :-) Thanks for the "schooling". Here is code to check on that within mapView if !(annotation.isKindOfClass(CustomMapPinAnnotation)) { println("Inside my mapview but was not passed Custom CustomMapPinAnnotation ") return nil } – Kokanee May 06 '15 at 16:45
  • "many moons ago in the beta days" I know just what you mean! The language has evolved a _lot_ since then. – matt May 06 '15 at 16:46