Skip to content

Instantly share code, notes, and snippets.

@FrankReh
Created January 20, 2017 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrankReh/41edd9fd2235127bf0d416ae4e4fdad3 to your computer and use it in GitHub Desktop.
Save FrankReh/41edd9fd2235127bf0d416ae4e4fdad3 to your computer and use it in GitHub Desktop.
Add string method to NSIndexSet (pretty print NSIndexSet)
#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