From fbd90ff48a861e06562263b211a69181263bb449 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Sun, 22 Mar 2015 10:43:10 -0700 Subject: [PATCH] safety checks & no-op returns --- platform/ios/MGLMapView.mm | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index d61c4dad4..2608e4a00 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -1576,6 +1576,8 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) - (void)addAnnotation:(id )annotation { + if ( ! annotation) return; + // The core bulk add API is efficient with respect to indexing and // screen refreshes, thus we should defer to it even for individual adds. // @@ -1584,6 +1586,8 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) - (void)addAnnotations:(NSArray *)annotations { + if ( ! annotations) return; + std::vector latLngs; latLngs.reserve(annotations.count); @@ -1594,6 +1598,8 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) for (id annotation in annotations) { + assert([annotation conformsToProtocol:@protocol(MGLAnnotation)]); + latLngs.push_back(coordinateToLatLng(annotation.coordinate)); NSString *symbolName = nil; @@ -1617,6 +1623,8 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) - (void)removeAnnotation:(id )annotation { + if ( ! annotation) return; + // The core bulk deletion API is efficient with respect to indexing // and screen refreshes, thus we should defer to it even for // individual deletes. @@ -1626,11 +1634,15 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) - (void)removeAnnotations:(NSArray *)annotations { + if ( ! annotations) return; + std::vector annotationIDsToRemove; annotationIDsToRemove.reserve(annotations.count); for (id annotation in annotations) { + assert([annotation conformsToProtocol:@protocol(MGLAnnotation)]); + annotationIDsToRemove.push_back([[self.annotationsStore objectForKey:annotation] unsignedIntValue]); [self.annotationsStore removeObjectForKey:annotation]; } @@ -1645,8 +1657,12 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) - (void)setSelectedAnnotations:(NSArray *)selectedAnnotations { + if ( ! selectedAnnotations.count) return; + id firstAnnotation = selectedAnnotations[0]; + assert([firstAnnotation conformsToProtocol:@protocol(MGLAnnotation)]); + if ( ! [self viewportBounds].contains(coordinateToLatLng(firstAnnotation.coordinate))) return; self.selectedAnnotation = firstAnnotation; @@ -1656,6 +1672,8 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) { (void)animated; + if ( ! annotation) return; + if ( ! [self viewportBounds].contains(coordinateToLatLng(annotation.coordinate))) return; self.selectedAnnotation = annotation; @@ -1665,6 +1683,8 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng) { (void)animated; + if ( ! annotation) return; + if ([self.selectedAnnotation isEqual:annotation]) self.selectedAnnotation = nil; }