I'm trying to save an array of locations to NSUserDefaults. First I've converted the CLLocationCoordinates into an array of NSValues to be saved using NSUserDefaults. Here is my code so far:
_locationsArray = [[NSMutableArray alloc] init];
NSUInteger count = [self.locations count];
CLLocationCoordinate2D coordinates[count];
for (NSInteger i = 0; i < count; i++) {
coordinates[i] = [(CLLocation *)self.locations[i] coordinate];
NSValue *locationValue = [NSValue valueWithMKCoordinate:coordinates[i]];
[_locationsArray addObject:locationValue];
}
NSLog(@"location = %@", _locationsArray);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:totalDistance forKey:@"totalDistance"];
[defaults setObject:_locationsArray forKey:@"mapOverlay"];
// [defaults setDouble:_totalTime forKey:@"totalTime"];
[defaults setObject:avgSpeedToBeSaved forKey:@"averageSpeed"];
[defaults setObject:totalCalories forKey:@"totalCalories"];
[defaults synchronize];
However I get the following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSUserDefaults setObject:forKey:]: attempt to insert non-property list object (
"<8b4df1d9 36ab4240 d5059bbe 47825ec0>",
"<f6dd639f 36ab4240 a2a7b7f5 49825ec0>",
"<f6dd639f 36ab4240 a2a7b7f5 49825ec0>",
"<20c46379 36ab4240 5ff02732 4c825ec0>",
"<20c46379 36ab4240 5ff02732 4c825ec0>",
"<d24c9c81 36ab4240 2bd75f9f 4e825ec0>",
"<d24c9c81 36ab4240 2bd75f9f 4e825ec0>",
"<d810b96c 36ab4240 f4d96401 51825ec0>",
"<d810b96c 36ab4240 f4d96401 51825ec0>"
) for key mapOverlay'
I thought that if I converted the coordinates to NSValues they would be fine to be saved to NSUserDefaults - Is that not the case? How else should I be doing this?