如何测试两个 CLLocation 的相等性
我遇到了 isEqual 问题:
代码:
if (currentAnchor isEqual:currentBusiness.getCllLocation))
{
do a;
}
else
{
do b;
}
currentanchor 和 currentbusiness.getCllocation 是位置
但是如果它们相同,为什么调用函数 b?我的代码有问题吗?
I'm having a problem with isEqual:
The code:
if (currentAnchor isEqual:currentBusiness.getCllLocation))
{
do a;
}
else
{
do b;
}
currentanchor and currentbusiness.getCllocation are locations
But if they are the same, why is function b called? Is something wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据
getClLocation
的名称,我假设这两个对象都是CLLocation
类型。CLLocation
没有任何关于其isEqual:
方法的规范,因此它可能只是继承NSObject
的实现,它只是比较对象的指针。如果您有两个具有相同数据的不同对象,则isEqual:
实现将返回NO
。如果你有两个不同的物体,只是位置略有不同,那么它们肯定是不相等的。比较位置对象时,您可能不需要
isEqual:
。相反,您可能希望在CLLocation
上使用distanceFromLocation:
方法。像这样的东西会更好:I assume both of these objects are of type
CLLocation
, based on the name ofgetClLocation
.CLLocation
doesn't have any specification on what itsisEqual:
method does, so it's likely just inheriting the implementation ofNSObject
, which simply compares the pointers of the objects. If you've got two distinct objects with identical data, thatisEqual:
implementation would returnNO
. And if you've got two distinct objects with just a slight variation in location, they definitely would not be equal.You probably don't want
isEqual:
when comparing location objects. Rather, you probably want to use thedistanceFromLocation:
method onCLLocation
. Something like this would be better:已经有一段时间了。
我对 BJ Homer 所做的与此类似。我只是添加这个。
我很惊讶我是问这个问题的人:)
It's been a while.
What I did is similar with BJ Homer. I just add this.
I was surprised I were the one asking this question :)
isEqual
仅检查对象而不检查其内容。您需要创建自己的方法,在其中访问对象的变量并使用==
运算符检查它们是否相等。isEqual
just check only objects not their content. you require to create your own method where you access the variables of object and check them for equality using==
operator.斯威夫特4.0版本:
Swift 4.0 version: