Wednesday, June 19, 2013

NSIndexPath compare example in Objective C (iOS).

NSIndexPath compare

Indicates the depth-first traversal order of the receiving index path and another index path.

- (NSComparisonResult)compare:(NSIndexPath *)indexPath

Parameters of [NSIndexPath compare]
Index path to compare.
This value must not be nil. If the value is nil, the behavior is undefined.

Return Value of [NSIndexPath compare]
The depth-first traversal ordering of the receiving index path and indexPath.

NSOrderedAscending: The receiving index path comes before indexPath.
NSOrderedDescending: The receiving index path comes after indexPath.
NSOrderedSame: The receiving index path and indexPath are the same index path.
NSIndexPath compare example.
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.

Example of [NSIndexPath compare].
NSComparisonResult result = [indexPath compare:[tempMutArray objectAtIndex:n];

if(result == NSOrderedSame) {
    // do stuff
In the Cocoa APIs, there are some non-class-types in use. Most important are those from the Foundation data types, which include

End of NSIndexPath compare example article.