Question

I want to subtract two latitudes from each other to find the shortest distance, but I get this error, "Arithmetic on pointer to interface 'NSNumber', which is not a constant size in non-fragile ABI" If I change the - to a + I get a different error "Invalid operands to binary expression ('NSNumber *' and 'NSNumber *')" I've tried using doubles and many combinations of things, but it just doesn't work.

NSNumber *userLatitude = [NSNumber numberWithDouble:43.55];//sample

NSArray *listOfCities = [managedObjectContext executeFetchRequest:request error:&error];
    for (CityList *item in listOfCities){
        NSLog(@"latitude is %@",item.latitude);  
        NSNumber *distanceLat =  userLatitude - item.latitude;

I will then insert them into a mutable array along with the longitudes and compare the distance. One possible solution using CLLocation would be

double distance = [usersCurrentLoc distanceFromLocation:otherLoc];

where usersCurrentLoc and otherLoc are both CLLocation variables. I also want use the latitude and longitudes individually so I can do some custom plotting, and they are also stored separately, so I'd like to figure out the correct data types and most efficient solution.

item.latitude comes from core-data with the data model type of double and X-code auto generated the CityList class with a property of NSNumber * latitude;

Was it helpful?

Solution

If you want subtract two NSNumbers, then use this

NSNumber *distanceLat = [NSNumber numberWithFloat:([userLatitude floatValue] - [item.latitude floatValue])];

OTHER TIPS

This:

NSNumber *distanceLat =  userLatitude - item.latitude;

needs to be:

NSNumber *distanceLat = @([userLatitude doubleValue] - item.latitude);

If item.latitude is also an NSNumber then you need to call doubleValue on it too.

NSNumber is an object. You can't do math on the object. You need to use doubleValue to get its value and then you need to wrap the result in a new NSNumber instance.

BTW - why bother with NSNumber here? Why not do:

double userLatitude = 43.55;

double distanceLat = userLatitude - item.latitude;
    CLLocationCoordinate2D newCoordinate = [newLocation coordinate];
    CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];

CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];

The above one can be used to find distance between two locations

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top