swiftmapkitnszombiemapkitannotation

Crashing MapKit


I am currently working on a MapKit app, which puts custom annotations (locations) on a MapKit MapView. However, something is going wrong with placing the pins. The pins are in fact displayed on the map and such, but the app kept crashing. So I ran zombies on it and it gave me this error: An Objective-C message was sent to a deallocated 'MKMarkerAnnotationView' object (zombie) at address: 0x1030ef600 which then pointed me further to: specialised ViewController.mapView(_:viewfor:)

I believe the piece of code responsible for it is this:

extension ViewController {
    //Setting color of marker and enabling callouts
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) - > MKAnnotationView ? {
        let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView.canShowCallout = true
        annotationView.calloutOffset = CGPoint(x: -5, y: 5)

        //Add button for user to see more info about location
        annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

        //Change marker color based on location
        if annotation is MKUserLocation {
            return nil
        } else {
            if annotation.subtitle! == "Excellent location" {
                annotationView.markerTintColor = UIColor.green
            } else {
                if annotation.subtitle! == "Good location" {
                    annotationView.markerTintColor = UIColor.orange
                } else {
                    if annotation.subtitle! == "Average location" {
                        annotationView.markerTintColor = UIColor.yellow
                    } else {
                        if annotation.subtitle! == "Neutral location" {
                            annotationView.markerTintColor = UIColor.red
                        } else {
                            annotationView.markerTintColor = UIColor.black
                        }
                    }
                }
            }
        }
        return annotationView
    }
}

Unfortunately I have not been able to extract the exact issue with this block, nor the zombie description. It seems to be an persistent issue with the MKMarkerAnnotationView.

//After the suggested edit, the previous issue seems (after extensive test runs) to be unaffected. However now it crashes for another reason, namely: An Objective-C message was sent to a deallocated 'MKMarkerAnnotationView' object (zombie) at address: 0x13c0bb800.****Responsible caller 0x104c9c903 and pointer 0x13c0bb800 of category MKMarkerAnnotationView. No additional description unfortunately. I have truly no idea which piece of code is now failing. The crash happens when I tap an annotation pin.

Perhaps any of you could point out to me what is going/went wrong?


Solution

  • So after quite some while, I just rewrote the entire piece of code and found that I screwed up with the MKMarkerAnnotationView and the if statements for the colours. Removed those and now it works perfectly. Going to try to use it with a switch statement, but so far it has solved the issue entirely. Question hereby closed.