2

when i write this method :

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *view =[self.mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];

    if (view == nil) {
        view =[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
    }
}

there is warning on my *view object.."Incompatible pointer types initialising MKPinAnnotationView * with an expression of type MKAnnotationView * "

what is the solution for this please

Nirav Gadhiya
  • 6,342
  • 2
  • 37
  • 76

1 Answers1

2

If you are sure that you'll get a correct type of annotation using dequeue, than you can cast types simply like this:

MKPinAnnotationView *view = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];

The point is that dequeueReusableAnnotationViewWithIdentifier returns id type which can be a pointer to any object. So the compiler warns you that types MKPinAnnotationView * and id might be incompatible.

FreeNickname
  • 7,398
  • 2
  • 30
  • 60
  • 1
    `dequeueReusableAnnotationViewWithIdentifier` is not deprecated. `dequeueReusableCellWithIdentifier` is for `UITableViewCell` objects and has _nothing_ to do with `MKAnnotationView`. `dequeueReusableCellWithIdentifier` is not deprecated either and the new `dequeueReusableCellWithIdentifier:forIndexPath:` method serves a slightly different purpose than `dequeueReusableCellWithIdentifier`. –  Nov 13 '13 at 12:54
  • How could I miss that? And especially how could I mix annotations and cells? Thank you very much! :) I'll remove it from the answer to avoid confusion. – FreeNickname Nov 14 '13 at 03:19