Search code examples
iosobjective-cnsnumbercllocation

subtracting latitudes with NSNumber type to find distance


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;


Solution

  • If you want subtract two NSNumbers, then use this

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