如何测试两个 CLLocation 的相等性

2024-01-02

我遇到了 isEqual 的问题:

代码:

 if (currentAnchor isEqual:currentBusiness.getCllLocation))
    {
        do a;
    }
    else
    {
        do b;
    }

currentanchor 和 currentbusiness.getCllocation 是位置

但如果它们相同,为什么还要调用函数 b 呢?我的代码有问题吗?


我假设这两个对象都是类型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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何测试两个 CLLocation 的相等性 的相关文章

随机推荐