Graphics Programs Reference
In-Depth Information
The protocol defines
coordinate
as a read-only property, which means there is a meth-
od named
coordinate
that returns a
CLLocationCoordinate2D
. While most
methods declared in the
MKAnnotation
protocol are optional, the
coordinate
meth-
od is required.
Switch to
BNRMapPoint.m
. (The keyboard shortcut for switching between the header
file and the implementation file is Command-Control-Up arrow.) Synthesize the proper-
ties and add the implementation for the initializer.
#import "BNRMapPoint.h"
@implementation BNRMapPoint
@synthesize coordinate, title;
- (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
{
self = [super init];
if (self) {
coordinate = c;
[self setTitle:t];
}
return self;
}
@end
new designated initializer. (This method may already be implemented by the template, so
make sure to replace its contents if that is the case.)
- (id)init
{
return [self initWithCoordinate:CLLocationCoordinate2DMake(43.07, -89.32)
title:@"Hometown"];
}
In our code, we declared
coordinate
as a
readonly
property just like the protocol
does. But we didn't have to. We could have just implemented a
coordinate
method
that returns a
CLLocationCoordinate2D
. The
MKAnnotation
protocol, like all
protocols, only dictates method signatures. As long as the signatures match exactly, the
conforming class can implement them however it wants.
As implemented,
BNRMapPoint
will return the values of its instance variables (
co-
ordinate
and
title
) when sent the messages
coordinate
and
title
from the
MKAnnotation
protocol. However, those methods could return a constant value or per-
form some logic, as in the following implementation:
- (NSString *)title