How do I remove all map annotations in swift 2

user4812000 picture user4812000 · Sep 29, 2015 · Viewed 22.3k times · Source

I had working code to remove all map annotations with a button, but after my update to xcode 7 I am running into the error:

Type 'MKAnnotation' does not conform to protocol 'SequenceType'

if let annotations = (self.mapView.annotations as? MKAnnotation){
    for _annotation in annotations {
        if let annotation = _annotation as? MKAnnotation {
            self.mapView.removeAnnotation(annotation)
        }
    }
}

Answer

vadian picture vadian · Sep 29, 2015

In Swift 2 annotations is declared as non optional array [MKAnnotation] so you can easily write

let allAnnotations = self.mapView.annotations
self.mapView.removeAnnotations(allAnnotations)

without any type casting.