NSArray element failed to match the Swift Array Element type in mapView.annotations loop

507 Views Asked by At

I want to zoom PinImages in mapView(when it zoom out or in), for this I do :

func resizePin(scale: CGFloat, image: UIImage) -> UIImage {

    var newSize = CGSizeMake(image.size.width * scale, image.size.height * scale)
    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    image.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
    var newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage
}

after in delegate method I find scale and want to use resizePin method inside the loop of mapView annotations, to resize them all

func mapView(mapView: MKMapView!, regionWillChangeAnimated animated: Bool) {
    oldZoomLatitude = mapView.region.span.latitudeDelta
    oldZoomLongitude = mapView.region.span.longitudeDelta

}

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool) {
    newZoomLatitude = mapView.region.span.latitudeDelta
    newZoomLongitude = mapView.region.span.longitudeDelta

    mapScale = (newZoomLongitude! / oldZoomLongitude!)
    println(mapScale)

    for annotation in mapView.annotations as [MKAnnotationView]  { // EXCEPTION - fatal error: NSArray element failed to match the Swift Array Element type
    }
}

dont get how to make it right, and what is a reason of exception. Any ideas?

1

There are 1 best solutions below

0
On

The mapView.annotations property is of type MKAnnotation. If you'd like to modify the view for each annotation, try the below code:

for annotation in mapView.annotations {
    let view = mapView.viewForAnnotation(annotation)
    // modify the view.
}