Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,710
6,304
Threw these categories together and felt like sharing:

Code:
@implementation NSString (subscript)

- (id)objectAtIndexedSubscript:(NSUInteger)index {
    return @([self characterAtIndex:index]);
}

- (id)objectForKeyedSubscript:(id)key {
    return [NSValue valueWithRange:[self rangeOfString:key]];
}

@end

@implementation NSMutableString (subscript)

- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx {
    if ([obj isKindOfClass:NSNumber.class]) {
        unichar ch = [obj unsignedShortValue];
        obj = [NSString stringWithCharacters:&ch length:1];
    }
    [self replaceCharactersInRange:NSMakeRange(idx, 1) withString:obj];
}

- (void)setObject:(id)obj atKeyedSubscript:(id)key {
    [self replaceOccurrencesOfString:key withString:obj options:0 range:NSMakeRange(0, self.length)];
}

@end

Alternatively, maybe objectAtIndexedSubscript: should return a single character long NSString...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.