Couple questions on geocode challenge


#1

I thought about how I might solve the geocode challenge, and initially thought I’d stick the code into the WhereamiAppDelegate class as with all the other delegated code so far. But then I figured that I would have to check to see which MapPoint called the reverse geocoder, which seemed inelegant, so I made the MapPoint instance the delegate instead. It seems to work well.

My questions are both just kind of style questions. First, am I violating any design principles by making a model object a delegate for another model object? Second, when should I release the geocoder I alloc in my MapPoint’s init function? Just leave it for dealloc, leaving that memory utilized until the end of the program but keeping it in one place, or put one release in didFindPlacemark and one in didFailWithError? Any possible conflicts that might arise?

Here’s my MapPoint.m code:

[code]@synthesize coordinate, title, subtitle;

  • (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t
    {
    self = [super init];
    if (self) {
    coordinate = c;
    [self setTitle:t];
    [self setSubtitle:@“Temporary subtitle!”];

      reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
      [reverseGeocoder setDelegate:self];
      [reverseGeocoder start];
    

    }
    return self;
    }

  • (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
    {
    NSString *pmDescrip = [[NSString alloc] initWithFormat:@"%@, %@", [placemark locality], [placemark administrativeArea]];

    NSLog(@“Found reverse geocode: %@”, pmDescrip);
    [self setSubtitle:pmDescrip];

    [reverseGeocoder release];
    [pmDescrip release];
    }

  • (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
    {
    NSLog(@“Could not find geolocation due to error: %@”, error);
    [self setSubtitle:@“Unknown location!”];

    [reverseGeocoder release];
    }[/code]


#2

Actually, your solution is very similar to the one that author Joe Conway provided for this challenge. He describes it in the forum for the 1st edition of this book in this thread: http://forums.bignerdranch.com/viewtopic.php?f=47&t=101

As you’ll see, he also releases the geocoder instance in both of the delegate methods.

Doug


#3

Ah, thanks, should have thought to look there…