isEqual doesn't always work for NSIndexPath? What can I use in its place?

梦想与她 提交于 2019-12-20 09:48:09

问题


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.

Here's the code:

- (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'

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


回答1:


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


Try [indexPath1 compare: indexPath2] == NSOrderedSame.

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.



来源:https://stackoverflow.com/questions/3791044/isequal-doesnt-always-work-for-nsindexpath-what-can-i-use-in-its-place

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!