hulab / ClusterKit

An iOS map clustering framework targeting MapKit, Google Maps and Mapbox.
MIT License
512 stars 86 forks source link

[Mapbox] Fatal error: NSArray element failed to match the Swift Array Element type #51

Closed azizkamoun closed 5 years ago

azizkamoun commented 5 years ago

Hi there, thank you for this wonderful library! I was hoping you could help resolve the following issue. It appears as though MKAnnotations in an NSArray do not match those in a Swift Array. Do you know why this could be the case?

func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
        guard let cluster = annotation as? CKCluster else {
            return nil
        }

        if cluster.count > 1 {
            for clusteredAnnotation in cluster.annotations { // Fatal error: NSArray element failed to match the Swift Array Element type
                print(clusteredAnnotation.coordinate)
            }

            return self.mapView.dequeueReusableAnnotationView(withIdentifier: CKMapViewDefaultAnnotationViewReuseIdentifier) ?? MBXAnnotationView(annotation: annotation, reuseIdentifier: CKMapViewDefaultAnnotationViewReuseIdentifier)
        }

        return mapView.dequeueReusableAnnotationView(withIdentifier: CKMapViewDefaultAnnotationViewReuseIdentifier) ??
            MBXAnnotationView(annotation: annotation, reuseIdentifier: CKMapViewDefaultAnnotationViewReuseIdentifier)
}
azizkamoun commented 5 years ago

Searching the web for this error in the context of MapKit has resolved the issue. Assigning cluster.annotations to an array of untyped objects bridges the Objective-C class to Swift.

if cluster.count > 1 {
        let clusterAnnotations: [AnyObject] = cluster.annotations

        for clusteredAnnotation in clusterAnnotations {
            print(clusteredAnnotation.coordinate)
        }
}