Search code examples
swiftmapkitxcode9.3

MapKit runtime error in mapView(_:didAdd:) with clustering


I have an unexpected error when implementing MKMapViewDelegate's method mapView(_:didAdd:). It is happening since Xcode 9.3 and was not happening with Xcode 9.2. When using clustering, I get a runtime error when using views array.

func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
    if let view = views.first { // Fatal error: NSArray element failed to match the Swift Array Element type
        print(type(of: view))
    }
}

As I understand it, the views array does not contain only MKAnnotationView instances as expected. Here is what views contains on crash.

enter image description here

I see that there is an MKClusterAnnotation instance inside. If this is true I get the error, but why is this happening? Am I doing something wrong?

This is easily reproductible by implementing the method on the sample from WWDC 2017 - Session 237 What's New in MapKit.


Solution

  • I have experienced the same problem. If you implement clustering and supply a mapView(_:didAdd:) method, you will crash - This is definitely an Apply framework bug. The issue is easily reproducible in iOS 11.3, but fortunately has been corrected in iOS 11.4.