Help with Whereami challenge


#1

Hello,

I need a little direction help with the challenge. I cannot get the mapPoints to archive correctly. I am getting an exception thrown at runtime when the home button is pressed and encodeWithCoder: is being called. I am getting an “unrecognized selector” as error message. Here is how I am trying to encode the MapPoint class:

[code]-(void) encodeWithCoder:(NSCoder *) encoder
{
double lon = coordinate.longitude;
double lat = coordinate.latitude;

NSLog(@"Coordinates: (%d,%d)",lon, lat);

[encoder encodeObject: title forKey: @"title"];
[encoder encodeDouble: lon forKey:@"longitude"];
[encoder encodeDouble: lat forKey:@"latitude"];
[encoder encodeObject: placemark forKey: @"placemark"];

}

-(id) initWithCoder:(NSCoder *) decoder
{
[super init];

[self setTitle: [decoder decodeObjectForKey:@"title"]];

double lat = [decoder decodeDoubleForKey:@"latitude"];
double lon = [decoder decodeDoubleForKey: @"longitude"];
CLLocationCoordinate2D newCoord = CLLocationCoordinate2DMake(lat, lon);
[self setCoordinate: newCoord];

[/code]

Help would be appriciated. Thanks!!


#2

Hi,

MKPlaceMark doesn’t conform to the NSCoding protocol so [encoder encodeObject: placemark forKey: @“placemark”]; isn’t finding an appropriate method.

You can see whether a class implements NSCoding by either looking at the documentation for the class or by checking at runtime with something like - if ([MKPlacemark conformsToProtocol:@protocol(NSCoding)])

The easiest way would probably be to pick the attributes you want to save and encode them individually as NSString’s and then reconstruct in the initWithCoder.

HTH
Gareth


#3

It didn’t even cross my mind that MKPlacemark couldn’t be encoded. Thanks for the direction. I understand what is needed.

Drew


#4

What’s interesting is that, while MKPlacemark doesn’t appear to conform to NSCoding per the docs (in iOS 4), it does conform via CLPlacemark in iOS 5. The catch? Even when I run a test app on iOS 4, I’m told it still conforms to the protocol! Wasn’t looking forward to using OS versions to make the call on whether to call up to the superclass. Hmm …