Skip to content

Instantly share code, notes, and snippets.

@pitiphong-p
Last active December 28, 2015 05:09
Show Gist options
  • Save pitiphong-p/7447416 to your computer and use it in GitHub Desktop.
Save pitiphong-p/7447416 to your computer and use it in GitHub Desktop.
NSIndexSet category to get the nth index in the index set. (0 based position)
@interface NSIndexSet (NthPosition)
- (NSUInteger)indexAtIndex_ppb:(NSUInteger)position;
@end
@implementation NSIndexSet (NthPosition)
- (NSUInteger)indexAtIndex_ppb:(NSUInteger)position {
__block NSUInteger index = NSNotFound;
if (position < self.count) {
__block NSUInteger count = 0;
[self enumerateRangesUsingBlock:^(NSRange range, BOOL *stop) {
if ((count + range.length) > position) {
index = range.location + (position - count);
}
count += range.location;
*stop = count > position;
}];
}
return index;
}
@end
@pitiphong-p
Copy link
Author

Any feedback is welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment