isEqual并非总是适用于NSIndexPath吗?我可以用它代替什么? [英] isEqual doesn't always work for NSIndexPath? What can I use in its place?

查看:93
本文介绍了isEqual并非总是适用于NSIndexPath吗?我可以用它代替什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码依赖于比较两个NSIndexPath,并根据它们的相等性或缺乏性(使用-isEqual)执行不同的代码。在大多数情况下,它都可以正常运行,但有时却不能。我已经使用调试器控制台在代码执行期间测试了两个索引路径,它们看起来和我一样。

I've got some code that relies on comparing two NSIndexPaths and executing different code based on their equality or lack thereof (using -isEqual). Most of the time it works properly, but sometimes it doesn't. I've used the debugger console to test the two indexpaths during code execution, and they look identical to me.

这里是代码:

- (BOOL)selectedStreetIsSameAsLastSelectedStreet
{
    return [self.indexPathOfSelectedStreet isEqual:self.previousObject.indexPathOfSelectedStreet];
}

这是执行代码期间的输出:

Here's the output during the execution of the code:

(gdb) po self.indexPathOfSelectedStreet
<NSIndexPath 0x60a0770> 2 indexes [26, 1]
(gdb) po self.previousObject.indexPathOfSelectedStreet
<NSIndexPath 0x55b4f70> 2 indexes [26, 1]
(gdb) p (BOOL)[self.indexPathOfSelectedStreet isEqual:self.previousObject.indexPathOfSelectedStreet]
$2 = 0 '\000'

我做错了什么,还是有另一种方式可以可靠地测试两个NSIndexPaths的相等性?

Am I doing something wrong, or is there another way I can reliably test the equality of two NSIndexPaths?

推荐答案

从iOS 5开始,您只需使用 isEqual: (请参阅评论) )

As of iOS 5 you can just use isEqual: (see comments)



尝试 [indexPath1比较:indexPath2] == NSOrderedSame

也许您在 NSIndexPath 中发现了一个错误。如果尝试使用已存在的路径创建新的 NSIndexPath ,则应改用该路径。因此 isEqual:可能只是比较指针而不是存储的实际索引。

Maybe you found a bug in NSIndexPath. If you try to create a new NSIndexPath with a path that already exists you should get that one instead. So isEqual: probably just compares the pointers and not the actual indices stored.

这篇关于isEqual并非总是适用于NSIndexPath吗?我可以用它代替什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆