Created
January 20, 2017 15:10
-
-
Save FrankReh/41edd9fd2235127bf0d416ae4e4fdad3 to your computer and use it in GitHub Desktop.
Add string method to NSIndexSet (pretty print NSIndexSet)
This file contains hidden or 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
#import <Foundation/Foundation.h> | |
NSString *indexSetToString(NSIndexSet *indexSet) | |
{ | |
NSMutableString *s = [[NSMutableString alloc] init]; | |
__block NSString *sep = @""; | |
[indexSet enumerateRangesUsingBlock: | |
^void (NSRange range, BOOL *stop) { | |
switch (range.length) { | |
case 0: | |
return; | |
case 1: | |
[s appendFormat:@"%@%ld", sep, range.location]; | |
break; | |
default: | |
[s appendFormat:@"%@%ld-%ld", | |
sep, | |
range.location, | |
range.location + range.length - 1]; | |
break; | |
} | |
sep = @" "; | |
}]; | |
return [NSString stringWithFormat:@"[%@]", s]; | |
} | |
@implementation NSIndexSet (NSIndexSetStringAddition) | |
- (NSString *)string { | |
return indexSetToString(self); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment