使用“ clusteringIdentifier”会导致应用程序崩溃

问题描述 投票:0回答:1

我在应用程序中使用MapKit,并放置了许多注释。我想让不必要的注释出队,以确保我的RAM使用率不会正常运行。但是,使用我当前的代码,尝试缩小时该应用程序将崩溃。对我来说,当我尝试“重新聚类”我的注释时,似乎发生了崩溃。

我已尝试在不使用“ dequeueReusableAnnotationView”的情况下实现地图,该方法可以按预期工作。但是,浏览地图的次数越多,它将使用的RAM越多,并且在某个时候它会变得缓慢。

这是我实际上正在工作的代表的代码。但是,这不会实现出队,因此不是可行的解决方案:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        //Keep it default for the user location
        if mapView.userLocation.isEqual(annotation) {
            return nil;
        }

        let annotationView = MKMarkerAnnotationView()
        annotationView.isEnabled = true
        annotationView.canShowCallout = true
        annotationView?.markerTintColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
        annotationView.clusteringIdentifier = identifier
        annotationView.glyphImage = #imageLiteral(resourceName: "PLACEHOLDER-glyph-icon")
        annotationView.titleVisibility = .hidden
        annotationView.subtitleVisibility = .hidden
        annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)

        return annotationView
    }
}

这是我的“ HouseMarker.swift”类:

import MapKit

class HouseMarker: NSObject, MKAnnotation {
    var title: String?
    var subtitle: String?
    var coordinate: CLLocationCoordinate2D

    init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate
    }
}

这是我的委托函数,它会导致我在“缩小”缩放时崩溃:

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        //Keep it default for the user location
        if mapView.userLocation.isEqual(annotation) {
            return nil;
        }

        let identifier = "HouseMarker"

        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKMarkerAnnotationView

        if annotationView == nil {
            annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView?.canShowCallout = true
            annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
            annotationView?.markerTintColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
            annotationView?.clusteringIdentifier = identifier
            annotationView?.glyphImage = #imageLiteral(resourceName: "PLACEHOLDER-glyph-icon")
            annotationView?.titleVisibility = .hidden
            annotationView?.subtitleVisibility = .hidden
        } else {
            annotationView?.annotation = annotation
        }

        return annotationView
    }
}

当我捏缩小时,应用程序冻结,这是我得到的错误:

2019-08-25 21:04:26.646836+0200 housing[474:160023] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[housing.HouseMarker memberAnnotations]: unrecognized selector sent to instance 0x28261dc70'
*** First throw call stack:
(0x1ad272ab8 0x1ac477d00 0x1ad18b108 0x1ad278464 0x1ad27a260 0x1bdf46cb8 0x1be0b92f4 0x1ad157124 0x1bdf27424 0x1be0b9efc 0x1bdf3354c 0x1bdf24094 0x1bdf23490 0x1bdf23870 0x1bd90c0fc 0x1bdf24d74 0x1bdf24f58 0x1da2a9668 0x1da2b1b28 0x1da2af424 0x1da2ae8f8 0x1da2a26bc 0x1da2a1de8 0x1da2a1bb8 0x1da6d4f44 0x1da6b41e8 0x1da77f3e8 0x1da781ba4 0x1ad202b54 0x1ad202ad0 0x1ad20238c 0x1ad1fd060 0x1ad1fc964 0x1af43dd8c 0x1da699758 0x100514470 0x1accb8fd8)
libc++abi.dylib: terminating with uncaught exception of type NSException
swift mapkit
1个回答
0
投票

您的委托方法无法处理所有可能性。之后

    if mapView.userLocation.isEqual(annotation) {
        return nil;
    }

对聚类注释执行相同的操作:

    if annotation is MKClusterAnnotation {
        return nil
    }

还要注意,在现代iOS中,您出队错误。您应该在地图视图中注册注释,这样出队的注释视图将永远不会为零。

© www.soinside.com 2019 - 2024. All rights reserved.