A collection of example source codes for c/c++ and ios and android platform. It also includes objective c.

Monday, June 17, 2013

NSIndexSet firstIndex example in Objective C (iOS).

NSIndexSet firstIndex

Returns either the first index in the index set or the not-found indicator.

- (NSUInteger)firstIndex

Return Value of [NSIndexSet firstIndex]
First index in the index set or NSNotFound when the index set is empty.

NSIndexSet firstIndex example.

I believe NSIndexSet stores its indexes using ranges, so there isn't necessarily a quick way to return the nth index. You could enumerate keeping a counter until your counter reaches your target index:

NSUInteger index = [indexSet firstIndex];

for (NSUInteger i = 0, target = 4; i < target; i++)
index = [indexSet indexGreaterThanIndex:index];
That should give you the 4th index. You could even add the method as a category method if you want: