3

I've inherited a project which throws this warning

Incompatible pointer types assigning to 'MKPinAnnotationView *' from 'MKAnnotationView *'

at this line

pinView=[[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
    }

I'd like to return the project without warnings so I'm hoping someone here has a quick answer

Full code:

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id  <MKAnnotation>)annotation {
    MKPinAnnotationView *pinView = nil; 

    NSUserDefaults *prefs=[NSUserDefaults standardUserDefaults];

    if(annotation != mapView.userLocation) 

    {

        static NSString *defaultPinID = @"com.invasivecode.pin";

        pinView = (MKPinAnnotationView *)[mapView  dequeueReusableAnnotationViewWithIdentifier:defaultPinID];


        if (!pinView) {
            pinView=[[[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
        }

    }   
    pinView.animatesDrop=YES;
    [mapView.userLocation setTitle:@"I am here"];
    [mapView.userLocation setSubtitle:[prefs objectForKey:@"CurrentLocationName"]];
    return pinView;        
}

Thanks!

2 Answers 2

3

The pinView variable is declared as an MKPinAnnotationView but that line creates an MKAnnotationView.

Change this line:

pinView=[[[MKAnnotationView alloc]initWithAnnotation...

to:

pinView=[[[MKPinAnnotationView alloc]initWithAnnotation...


You should also have an else part to that if to handle annotation view re-use:

else
    pinView.annotation = annotation;
2

You are dequeing the pin annotation view and along with that you are allocating the annotation view as your pinview which was technically wrong!!! That's why it was firing the warning I guess. Try this may solve your issue.

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation: (id  <MKAnnotation>)annotation {


pinView = (MKPinAnnotationView *)[mapView  dequeueReusableAnnotationViewWithIdentifier:defaultPinID];


if (!pinView) {
        pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];
    }

..........
..........

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.