Note that there are some explanatory texts on larger screens.

plurals
  1. POMKPinAnnotationView and custom classes conforming to the MKAnnotation protocol
    text
    copied!<p>I have created the following method for determining the view for my annotations in a map view. </p> <pre><code>- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation { if ([annotation isKindOfClass:[MKUserLocation class]]) { return nil; } MKPinAnnotationView *pin; if ([annotation isKindOfClass:[AnnotationsWithIndices class]]) { int i = [(AnnotationsWithIndices *)annotation index]; if (i &gt; currentCheckpointIndex ) { pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:@"unvisited"]; if (!pin) { pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"unvisited"]; } [pin setPinColor:MKPinAnnotationColorRed]; } if (i == currentCheckpointIndex) { pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:@"current"]; if (!pin) { pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"]; } [pin setPinColor:MKPinAnnotationColorGreen]; } if (i &lt; currentCheckpointIndex) { pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:@"visited"]; if (!pin) { pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"visited"]; } [pin setPinColor:MKPinAnnotationColorPurple]; } [pin setAnimatesDrop:YES]; return [pin autorelease]; } else return nil; } </code></pre> <p>The idea is that I want the different annotation views (pins) be of different colors depending on whether the user has visited them to signal which annotation to visit next. </p> <p>This code works fine, but I have a few questions about it which I hope someone can answer. </p> <p>Firstly, the MKPinAnnotations are dequeued from a map view and reused whenever possible. In the line doing so (which I have found in multiple blogs and forums) </p> <pre><code>pin = (MKPinAnnotationView *)[mv dequeueReusableAnnotationViewWithIdentifier:@"unvisited"]; </code></pre> <p>I understand that the return value of the <code>dequeueReusableAnnotationViewWithIdentifier:</code> is an instance of MKAnnotationView, whereas <code>pin</code> is a pointer to an instance of MKPinAnnotationView (which is a subclass o MKAnnotationView). This, I guess, is why there is a 'cast' seemingly going on with the prefix <code>(MKPinAnnotationView *)</code> in front of the method call. Is this really a cast and in this case, is it not dangerous since the MKPinAnnotationView contains more instance variables (than MKAnnotationView) and hence more space in memory?</p> <p>I have tried to find some information about this, but nowhere I have not found anyone explaining this in particular.</p> <p>Furthermore, the pointer annotation is either of class MKUserLocation or of my own custom class <code>AnnotationsWithIndices</code> conforming to the MKAnnotation protocol. Now, in order to determine which color the annotation view should have I have added an instance variable called index into the <code>AnnotationsWithIndices</code> class. Now, when I call the getter for the index I write </p> <pre><code>int i = [(AnnotationsWithIndices *)annotation index]; </code></pre> <p>Now, I basically have the same question about this. Is there a cast going on here or is it just to let the compiler know that it is ok to send the message index to annotation? I guess that the compiler expects annotation to be an id whereas it really is a pointer to an instance of AnnotationsWithIndices. Of course I know that this is the case since the annotation will be of my custom class, and I also check this explicitly to be sure. Is the <code>(AnnotationsWithIndices)</code> there only to signal to the compiler that this is OK? </p> <p>I have also tried to find information about this without luck. </p> <p>I am very thankful for any answers on this. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload