Wednesday, June 19, 2013

NSIndexPath indexPathByRemovingLastIndex example in Objective C (iOS).

NSIndexPath indexPathByRemovingLastIndex

Provides an index path with the indexes in the receiving index path, excluding the last one.

- (NSIndexPath *)indexPathByRemovingLastIndex

Return Value
New index path with the receiving index path’s indexes, excluding the last one.

Discussion of [NSIndexPath indexPathByRemovingLastIndex]
Returns an empty NSIndexPath instance if the receiving index path’s length is 1 or less.

Special Considerations
On OS X v10.4 this method returns nil when the length of the receiving index path is 1 or less. On iOS and OS X v10.5 and later this method will never return nil.

NSIndexPath indexPathByRemovingLastIndex example.
You can try this - perhaps equally clunky, but at least a bit shorter:

NSInteger newLast = [indexPath indexAtPosition:indexPath.length-1]+1;
indexPath = [[indexPath indexPathByRemovingLastIndex] indexPathByAddingIndex:newLast];

End of NSIndexPath indexPathByRemovingLastIndex example article.