开发者

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

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.previousObjec开发者_开发技巧t.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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜