Trying to understand this


#1

Hi,

When reading the explanation of the piece of code below I have a question on the coordinate ‘getter’ message.
MKUserLocation conforms to the protocol MKAnnotation. I understand that part. Whenever I conform to something and want to use a method from the class I conform I need to implement this method first. But when a existing defined class like MKUserLocation conforms to an existing defined class Like MKAnnotation (as you can see in the documentation) does this also mean that the getter method of the property ‘coordinate’ (as used in the code below) in MKAnnotation is already implemented in MKUserLocation.m?

-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldview setRegion:region animated:YES];
}

#2

Yes, with an asterisk.

The asterisk is this: the protocol specifies that the class must define a coordinate method, but whether that method is defined as a “getter” for a property, or instead is a dynamic method that applies logic to identify and/or construct a coordinate object, is entirely up to the class.


#3

Ah! This clears up some confusion on my part. I had come up with

and I was wondering why the code in the book worked, since I didn’t see “coordinate” listed directly in the MKUserLocation documentation… (I tried both). Turns out it’s because the book uses the coordinate method from the MKAnnotation protocol. Moral of the story is “read, don’t skim”!