MKPolyline & # 8594; NSKeyedArchiver & # 8594; NSData SIGABRT

In my application, I am trying to save an MKPolylines array to NSUserDefaults .

 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:overlays]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"theKey"]; 

gives:

 [MKPolyline encodeWithCoder:]: unrecognized selector sent to instance 0x169c20` Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [MKPolyline encodeWithCoder:]: unrecognized selector sent to instance 0x1c57e0' 

Edit: I have made some progress. MKPolylineView follows the NSCoding protocol, so I converted the MKPolyline array to MKPolylineView s array. The problem is that when I want to add them back to the map later, I cannot convert them back to MKPolyline s. Any idea how to do this?

Failure with this code:

 NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:@"theKey"]; NSArray* overlays = [NSKeyedUnarchiver unarchiveObjectWithData:data]; for(MKPolylineView* a in overlays) [mapView addOverlay:a.overlay]; 2011-10-17 21:15:56.416 Trail Tracker[4269:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack: (0x34b2f8bf 0x36c3a1e5 0x34a8420f 0x35697595 0x6257 0x62db 0x365f77ff 0x36601d53 0x36601cc1 0x366a1339 0x366a014f 0x366fad97 0x649b 0x36671565 0x366e9ce7 0x31fcc943 0x34b03a63 0x34b036c9 0x34b0229f 0x34a854dd 0x34a853a5 0x351f9fed 0x365ec743 0x2c75 0x2c34) terminate called throwing an exception(gdb) 0x36601d53 0x36601cc1 0x366a1339 0x366a014f 0x366fad97 0x649b 0x36671565 0x366e9ce7 0x31fcc943 0x34b03a63 0x34b036c9 0x34b0229f 0x34a854dd 0x34a853a5 0x351f9fed 0x365ec743 0x2c75 0x2c34) NSData* data = [[NSUserDefaults standardUserDefaults] objectForKey:@"theKey"]; NSArray* overlays = [NSKeyedUnarchiver unarchiveObjectWithData:data]; for(MKPolylineView* a in overlays) [mapView addOverlay:a.overlay]; 2011-10-17 21:15:56.416 Trail Tracker[4269:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil' *** First throw call stack: (0x34b2f8bf 0x36c3a1e5 0x34a8420f 0x35697595 0x6257 0x62db 0x365f77ff 0x36601d53 0x36601cc1 0x366a1339 0x366a014f 0x366fad97 0x649b 0x36671565 0x366e9ce7 0x31fcc943 0x34b03a63 0x34b036c9 0x34b0229f 0x34a854dd 0x34a853a5 0x351f9fed 0x365ec743 0x2c75 0x2c34) terminate called throwing an exception(gdb) 
+4
source share
2 answers

I'm not very sure about this, but MKPolylines are made from the CLLocationCoordinate2D array, which contains the float value for lat and long.

So, if you can convert this CLLocationCoordinate2D array into a dictionary array, I think you can save these lines in your default settings.

You can do something like this

 MKMapPoint *points = overlays.points; NSMutableArray *temp = [NSMutableArray array]; for(int i = 0; i < points.length; i++) { // Not sure for this part NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:points[i].x], @"x", [NSNumber numberWithFloat:points[i].y], @"y", nil]; [temp addObject:dict]; } 

You can then use this array to store overlap points in nsuserdefaults such as

 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:temp]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"theKey"]; 
+3
source

From a look at the inheritance hierarchy NSObject → MKShape → MKMultiPoint → MKPolyline, none of them comply with the NSCoding protocol. Thus, you can consider the inheritance of MKPolyline and enable the NSCoding protocol and implement the required method and use it.

0
source

All Articles