Alex Rivera | Logout

NSKeyedArchiver fails with CLLocationCoordinate2D structs. Why?

Asked 2012-09-05T23:52:25.473
10

I don't understand why I can archive CGPoint structs but not CLLocationCoordinate2D structs. What's the difference to the archiver?

Platform is iOS. I'm running in the simulator and haven't tried on the device.

// why does this work:
NSMutableArray *points = [[[NSMutableArray alloc] init] autorelease];
CGPoint p = CGPointMake(10, 11);
[points addObject:[NSValue valueWithBytes: &p objCType: @encode(CGPoint)]];
[NSKeyedArchiver archiveRootObject:points toFile: @"/Volumes/Macintosh HD 2/points.bin" ];

// and this doesnt work:
NSMutableArray *coords = [[[NSMutableArray alloc] init] autorelease];
CLLocationCoordinate2D c = CLLocationCoordinate2DMake(121, 41);
[coords addObject:[NSValue valueWithBytes: &c objCType: @encode(CLLocationCoordinate2D)]];
[NSKeyedArchiver archiveRootObject:coords toFile: @"/Volumes/Macintosh HD 2/coords.bin" ];

I get a crash on the 2nd archiveRootObject and this message is printed to the console:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKeyedArchiver encodeValueOfObjCType:at:]: this archiver cannot encode structs'
Edit
Report

1 Answer

0

It's because @encode chokes on CLLocationCoordinate2D

NSLog(@"coords %@; type: %s", coords, @encode(CLLocationCoordinate2D)); yields coords ( "<00000000 00405e40 00000000 00804440>" ); type: {?=dd}

answered 2012-09-06T00:09:06.927

Your Answer