我想互相减去两个纬度以找到最短的距离,但是我收到此错误,"指向接口'NSNumber'的指针上的算术,在非脆弱的 ABI 中不是恒定大小" 如果我将 - 更改为 + 我会收到不同的错误"二进制表达式的操作数无效('NSNumber *' 和 'NSNumber *')" 我尝试使用双精度和许多事物的组合, 但它就是行不通。
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;
然后,我将将它们与经度一起插入到一个可变数组中并比较距离。使用 CLLocation 的一种可能的解决方案是
double distance = [usersCurrentLoc distanceFromLocation:otherLoc];
其中 usersCurrentLoc 和 otherLoc 都是 CLLocation 变量。我还想单独使用纬度和经度,以便我可以进行一些自定义绘图,并且它们也单独存储,因此我想找出正确的数据类型和最有效的解决方案。
item.latitude 来自数据模型类型为 double 的核心数据,X 代码自动生成了属性为 NSNumber * latitude 的 CityList 类;
如果你想减去两个 NSNumber,那么使用这个
NSNumber *distanceLat = [NSNumber numberWithFloat:([userLatitude floatValue] - [item.latitude floatValue])];
这个:
NSNumber *distanceLat = userLatitude - item.latitude;
需要:
NSNumber *distanceLat = @([userLatitude doubleValue] - item.latitude);
如果item.latitude
也是一个NSNumber
那么你也需要打电话给doubleValue
。
NSNumber
是一个对象。你不能对对象做数学运算。您需要使用 doubleValue
来获取其值,然后需要将结果包装在新的 NSNumber
实例中。
顺便说一句 - 为什么要在这里打扰NSNumber
?为什么不做:
double userLatitude = 43.55;
double distanceLat = userLatitude - item.latitude;
CLLocationCoordinate2D newCoordinate = [newLocation coordinate];
CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];
CLLocation距离米 = [新位置距离从位置:旧位置];
上面的一个可以用来查找两个位置之间的距离