如何比较两个NSIndexPaths? [英] How to compare two NSIndexPaths?
问题描述
我正在寻找一种方法来测试索引路径是否相等,并且相等我的意思是在每个级别上相等?我试过比较:
但它似乎不起作用,与 NSOrderedSame
相比,我总是如此,即使索引是绝对不一样。
I'm looking for a way to test if to index paths are equal, and by equal I mean equal on every level? I tried compare:
but it seems not to work, I always get true when compared with NSOrderedSame
even if the indexes are definitely not the same.
推荐答案
几乎所有的Objective-C对象都可以使用进行比较isEqual:
方法。所以,为了测试相等性,你只需要 [itemCategoryIndexPath isEqual:indexPath]
,你就可以了。现在,这是因为 NSObject
实现 isEqual:
,所以所有对象都自动拥有该方法,但是如果某个类没有'覆盖它, isEqual:
只会比较对象指针。
Almost all Objective-C objects can be compared using the isEqual:
method. So, to test equality, you just need [itemCategoryIndexPath isEqual:indexPath]
, and you're good to go. Now, this works because NSObject
implements isEqual:
, so all objects automatically have that method, but if a certain class doesn't override it, isEqual:
will just compare object pointers.
在 NSIndexPath
,由于 isEqual:
方法已被覆盖,因此您可以按预期方式比较对象。但是,如果我要编写一个新类 MyObject
而不重写该方法, [instanceOfMyObject isEqual:anotherInstanceOfMyObject]
将实际上与 instanceOfMyObject == anotherInstanceOfMyObject
相同。
In the case of NSIndexPath
, since the isEqual:
method has been overridden, you can compare the objects as you were to expect. But if I were to write a new class, MyObject
and not override the method, [instanceOfMyObject isEqual:anotherInstanceOfMyObject]
would effectively be the same as instanceOfMyObject == anotherInstanceOfMyObject
.
你可以在 NSObject中阅读更多内容协议参考。
这篇关于如何比较两个NSIndexPaths?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!