groups

Link to this snippet:


Download to Code Collector

language: Objective-C
licence: Other

NSIndexSet to String

options: send to code collectorview all myztikjenz's snippets
@interface NSIndexSet (NSIndexSet_Additions) 
// Converts an index set to a string with format (1,2-5,9)
-(NSString *) indexSetToString;
@end

// Shh... undocumented methods.  Don't tell anyone.
@interface NSIndexSet (Undocumented_NSIndexSet)
- (unsigned int)rangeCount;
- (struct _NSRange)rangeAtIndex:(unsigned int)fp8;
@end

@implementation NSIndexSet (NSIndexSet_Additions)

-(NSString *) indexSetToString
{
	NSMutableString *str = [NSMutableString stringWithCapacity:10];
	
	int x;
	for( x=0; x<[self rangeCount]; x++ )
	{
		NSRange range = [self rangeAtIndex:x];
		
		if( x>0 )
			[str appendString:@","];
		
		if( range.length == 1 )
			[str appendFormat:@"%d", range.location];
		else
			[str appendFormat:@"%d-%d", range.location, NSMaxRange( range ) - 1];
	}
	
	return( str );
}

@end