Threw these categories together and felt like sharing:
Alternatively, maybe objectAtIndexedSubscript: should return a single character long NSString...
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...