33

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.

Valentin Radu
  • 8,353
  • 8
  • 57
  • 89

2 Answers2

90

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.

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.


You can read more in the NSObject Protocol Reference.

Itai Ferber
  • 22,011
  • 5
  • 56
  • 67
  • @Itai Ferber, how do one may know, if a method has been overridden or not. – Vasu Jan 04 '13 at 12:44
  • @Kailash You can't necessarily know if a method has been overridden. I think there may be a way, but I'm not sure; it's not within the realm of everyday programming - it would take some messing around with the runtime. Anyway, for the most part, there'd be no reason to need to know whether a class overrides this method, because it's providing you with functionality you need. Always use `isEqual:` on objects, and let the class itself handle the problem for you. – Itai Ferber Jan 04 '13 at 15:00
  • 1
    +1 I did not know isEqual: compared pointers when not implemented. Good to know! – LilDwarf Apr 26 '13 at 21:44
10

In Swift you use == to compare if NSIndexPaths are the same.

import UIKit

var indexPath1 = NSIndexPath(forRow: 1, inSection: 0)
var indexPath2 = NSIndexPath(forRow: 1, inSection: 0)
var indexPath3 = NSIndexPath(forRow: 2, inSection: 0)
var indexPath4 = indexPath1

println(indexPath1 == indexPath2) // prints "true"
println(indexPath1 == indexPath3) // prints "false"
println(indexPath1 == indexPath4) // prints "true"

println(indexPath1 === indexPath2) // prints "true"
println(indexPath1 === indexPath3) // prints "false"
println(indexPath1 === indexPath4) // prints "true"

Swift uses == for value comparisons. === is used for detecting when two variables reference the exact same instance (location in memory etc). Interestingly, the indexPath1 === indexPath2 shows that NSIndexPath is built to share the same instance whenever the values (section row) match, so even if you were comparing instances, it would still be valid.

This answer taken almost completely from this fantastic SO answer from drewag and reproduced here since this is the first Google result for 'compare indexpath' and we are not supposed to just paste a link.

Community
  • 1
  • 1
Joshua Dance
  • 6,602
  • 3
  • 52
  • 60
  • 1
    This question is almost 4 years old, is tagged objective-c, and has an accepted answer. I think it would be better to post this as an answer to a separate question. – sbooth Mar 06 '15 at 23:49
  • 3
    Just trying to help future rookies like me. This is the #1 result when you Google 'compare indexPath'. I will ask a separate question and post this answer but people are going to be finding this first for a long time. :) – Joshua Dance Mar 07 '15 at 00:17
  • 1
    Actually the question was already asked, like I mentioned in my answer. http://stackoverflow.com/questions/25172525/comparing-nsindexpath-swift - But even the onsite search doesn't show that question when you search for 'compare indexpath swift'. It just shows this answer. So I am going to leave my answer here to point people there. – Joshua Dance Mar 07 '15 at 00:25
  • 1
    Now that you posted the link to the other question it shows up in the side bar. Hopefully it will help someone in the future like you said- the objective-c and swift waters are definitely muddled. – sbooth Mar 07 '15 at 02:59
  • 3
    This helped me bro. I'm looking for Swift and got to this post. – Edward Anthony Jul 22 '15 at 22:40