0

I am working on swift 2.1 xcode 7 , i have to add multiple Annotations in map view But it's warning

cast from NSMutableArray to unrelated type MKAnnotation always fails

on this code

mapView.addAnnotations(aryNotation as! [MyAnnotation])

 for var i = startPoint; i <= endPoint; i++
        {

          let aryNotation:NSMutableArray = NSMutableArray()

            let locationAnnotation = MyAnnotation(coordinate: location,
                title: p_user_name!,
                subtitle: "",
                pinColor: colorofpin,
                getTag:i)


            /* And eventually add them to the map */

            aryNotation.addObject(locationAnnotation)




        }
         mapView.addAnnotations(aryNotation as! [MyAnnotation])

2nd is when i set map pin image is not working but my code is properly working in swift 2.0 but when i update my code on swift 2.1 latest xcode 7 is not working here is my code for second issue

 func mapView(mapView: MKMapView,
        viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView?{

            if annotation is MyAnnotation == false{
                return nil
            }

            /* First typecast the annotation for which the Map View has
            fired this delegate message */
            let senderAnnotation = annotation as! MyAnnotation

            /* We will attempt to get a reusable
            identifier for the pin we are about to create */
            let pinReusableIdentifier = senderAnnotation.pinColor.rawValue

            /* Using the identifier we retrieved above, we will
            attempt to reuse a pin in the sender Map View */
            var annotationView =
            mapView.dequeueReusableAnnotationViewWithIdentifier(
                pinReusableIdentifier) as? MKPinAnnotationView


                /* If we fail to reuse a pin, then we will create one */
                annotationView = MKPinAnnotationView(annotation: senderAnnotation,
                    reuseIdentifier: pinReusableIdentifier)

               //print("senderAnnotation.getTag = = \(senderAnnotation.getTag)")
               annotationView?.tag = senderAnnotation.getTag


                annotationView!.canShowCallout = true

                if(currentIndex == senderAnnotation.getTag)
                {
                    //print("currentIndex===\(currentIndex)")
                    let pinImage = UIImage(named:"jivemap_pin_o")
                    annotationView!.image = pinImage
                    annotationView!.layer.zPosition = 1
                }else
                {
                let pinImage = UIImage(named:"jivemap_pin_g")
                    annotationView!.image = pinImage
                }


            return annotationView

    }

Maybe both issue are related , Please share your valuable knowledge Regards, Nishant chandwani

vadian
  • 274,689
  • 30
  • 353
  • 361

1 Answers1

0

Declare aryNotation as native type [MyAnnotation] rather than a quite unspecified NSMutableArray, because the types are not related.

var aryNotation = [MyAnnotation]()
vadian
  • 274,689
  • 30
  • 353
  • 361
  • i cant able to understand please describe some more let aryNotation:NSMutableArray = NSMutableArray() i have describe like this – Nishant Chandwani Dec 29 '15 at 12:20
  • The Problem is addObject is not working , because after this code this is not a mutable array – Nishant Chandwani Dec 30 '15 at 15:08
  • it is definitely a mutable array, but for a Swift array you have to use the syntax `aryNotation.append(locationAnnotation)` – vadian Dec 30 '15 at 15:12
  • I'm not that familiar with MapKit. Look for sample code in Swift 2.1 of the `viewForAnnotation` method. The lines to reuse the view don't look right. There is probably an `if - else` missing like `if there is a reusable view, use it else create a new one` – vadian Dec 30 '15 at 15:35
  • I'm not "followable" ;-) – vadian Dec 30 '15 at 15:41