I am trying to remove overlays from map.
func removeMapOverlay() {
var removeOverlays : [AnyObject]! = self.mapView.overlays
// Above line throws runtime exception
self.mapView.removeOverlays(removeOverlays)
}
self.mapView.overlays
are type of AnyObject
array. var overlays: [AnyObject]! { get }
.
So initially I wrote
var removeOverlays = self.mapView.overlays
It throws EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
exception at this line on runtime.
So I did type casting for [AnyObject]
I don't know it is correct or not but it still gives me same exception at runtime.
Edit:
What I did for Objective C code was:
- (void) removeMapOverlay {
[self.mapView removeOverlays:[self.mapView overlays]];
NSMutableArray *tempArray = [NSMutableArray arrayWithArray:[self.mapView annotations]];
if ([tempArray containsObject:[MKUserLocation class]]) {
[tempArray removeObject:[MKUserLocation class]];
}
NSArray *annotationArray = [NSArray arrayWithArray:tempArray];
tempArray = nil;
[self.mapView removeAnnotations:annotationArray];
}
I tried to create similar method in Swift. But it throws an exception like I explain above.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…