Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to save latitude and longitude to Core data causes bad access

Tags:

ios

core-data

I am trying to save latitude and longitude in core data, but causing a bad access. here is the code:

EDIT :

Favourites *fav = (Favourites *)[NSEntityDescription insertNewObjectForEntityForName:@"Favourites" inManagedObjectContext:_managedObjectContext];

NSString *str = [NSString stringWithFormat:@"%@",point.title];
int index =[sharedRequest.name indexOfObject:str];
[fav setPlaceName:str];

double lat = [[sharedRequest.latArray objectAtIndex:index] doubleValue];
double lng = [[sharedRequest.lngArray objectAtIndex:index] doubleValue];

[fav setPlaceLatitude:lat];
[fav setPlaceLongitude:lng];

I don't know why this happening. Any help would be appreciable.

P.S lat and lng has value when I am printing them

like image 613
Deepak Khiwani Avatar asked Dec 05 '25 15:12

Deepak Khiwani


1 Answers

You need to save your latitude and longitude value as a NSNumber instead of a double.

NSNumber *latitude = [NSNumber numberWithDouble:lat];
[fav setPlaceLatitude:latitude];

Update

For your Favorites entity, placeLatitude attribute should be a double. However, when using the NSManagedObject subclass Favorites, you need to use NSNumber to set placeLatitude. Core Data uses NSNumber to store primitives such as int, float, double, bool.

Let Xcode generate your Favorites class. Pick the Core Data data model on the left pane, then Xcode -> Editor -> Create NSManagedObject Subclass.

like image 173
bbarnhart Avatar answered Dec 08 '25 18:12

bbarnhart