如何测试两个CLLocations的相等性

use*_*951 10 xcode objective-c iphone-4

我遇到了isEqual的问题:

代码:

 if (currentAnchor isEqual:currentBusiness.getCllLocation))
    {
        do a;
    }
    else
    {
        do b;
    }
Run Code Online (Sandbox Code Playgroud)

currentanchor和currentbusiness.getCllocation是位置

但如果它们是相同的,为什么函数b被称为?我的代码有问题吗?

BJ *_*mer 27

我假设这两个对象都是类型的CLLocation,基于名称getClLocation.

CLLocation没有任何关于它的isEqual:方法做什么的规范,所以它可能只是继承了实现NSObject,它简单地比较了对象的指针.如果您有两个具有相同数据的不同对象,则该isEqual:实现将返回NO.如果你有两个不同的物体,只是位置略有变化,它们绝对不会相等.

isEqual:比较位置对象时可能不需要.相反,您可能想要使用该distanceFromLocation:方法CLLocation.这样的事情会更好:

CLLocationDistance distanceThreshold = 2.0; // in meters
if ([currentAnchor distanceFromLocation:currentBusiness.getCllLocation] < distanceThreshold)
{
  do a;
}
else
{
  do b;
}
Run Code Online (Sandbox Code Playgroud)