Self.mapView.overlays返回nil而不是[],
这会导致撞车。
崩溃函数:

fileprivate func addRadiusToUserLocation(userCoordinate: CLLocationCoordinate2D,radius : Double) {
    let radiusCircle = MKCircle(center: userCoordinate, radius: radius)
    self.mapView.removeOverlays(self.mapView.overlays)
    self.mapView.addOverlays([radiusCircle])
}

堆栈:
Fatal Exception: NSInvalidArgumentException
*** -[__NSSetM removeObject:]: object cannot be nil
0  CoreFoundation                 0x18307bd38 __exceptionPreprocess
1  libobjc.A.dylib                0x182590528 objc_exception_throw
2  CoreFoundation                 0x183014c44 _CFArgv
3  CoreFoundation                 0x182f46264 -[__NSCFString hash]
4  VectorKit                      0x193139ca8 -[VKRasterOverlayTileSource removeOverlay:]
5  VectorKit                      0x192dc74b8 -[VKMapModel removeRasterOverlay:]
6  MapKit                         0x19342b7f0 -[MKOverlayContainerView _removeDrawable:forOverlay:level:]
7  MapKit                         0x19342acd0 -[MKOverlayContainerView removeOverlay:]
8  MapKit                         0x19342adc8 -[MKOverlayContainerView removeOverlays:]
9                        0x10429bfd8 SiteMapManager.addRadiusToUserLocation(userCoordinate:radius:) (SiteMapManager.swift:156)
10                       0x10429d184 specialized SiteMapManager.addOrUpdateUserLocation(userLocation:radius:) (SiteMapManager.swift:48)
11                       0x10429f1cc specialized SiteMapManager.mapView(_:viewFor:) (SiteMapManager.swift:203)
12                       0x10429c164 @objc SiteMapManager.mapView(_:viewFor:) (SiteMapManager.swift)
13 MapKit                         0x1933e99e8 -[MKMapView annotationManager:representationForAnnotation:]
14 MapKit                         0x1933a7ba4 -[MKAnnotationManager _addRepresentationForAnnotation:]
15 MapKit                         0x1933a68f0 -[MKAnnotationManager updateVisibleAnnotations]
16 MapKit                         0x1933c1620 -[MKMapView _didChangeRegionMidstream:]
17 MapKit                         0x1933dddf0 -[MKMapView mapLayer:didChangeRegionAnimated:]
18 VectorKit                      0x192d90690 -[VKMapView map:didChangeRegionAnimated:]
19 VectorKit                      0x192d3fc74 -[VKMapCameraController rotateToYaw:withPoint:animated:]
20 VectorKit                      0x192df7d84 -[VKMapCameraController snapMapIfNecessary:]
21 MapKit                         0x19342b7f0 -[MKOverlayContainerView _removeDrawable:forOverlay:level:]
22 MapKit                         0x19342acd0 -[MKOverlayContainerView removeOverlay:]
23 MapKit                         0x19342adc8 -[MKOverlayContainerView removeOverlays:]
24                       0x10429bfd8 SiteMapManager.addRadiusToUserLocation(userCoordinate:radius:) (SiteMapManager.swift:156)

addRadiusToUserLocation调用:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

地图视图:
@IBOutlet var mapView: MKMapView!

谢谢您。

最佳答案

它看起来像一个线程问题。
我可以通过将addoverlay和removeoverlay移动到主线程来修复它。

fileprivate func addRadiusToUserLocation(userCoordinate:
DispatchQueue.main.async {
   CLLocationCoordinate2D,radius : Double) {
   let radiusCircle = MKCircle(center: userCoordinate, radius: radius)
   self.mapView.removeOverlays(self.mapView.overlays)
   self.mapView.addOverlays([radiusCircle])
}

}

关于ios - MKmapView.overlays返回nil-崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47494524/

10-12 04:22