Last active
December 28, 2015 05:09
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any feedback is welcome :)