在解析JSON的时候,经常JSON的变量名都是蛇行式(下划线)命名法来命名的,所以写了一个类方法来转换。

+ (NSString *)convertToCamelCaseFromSnakeCase:(NSString *)key {
    NSMutableString *str = [NSMutableString stringWithString:key];
    while ([str containsString:@"_"]) {
        NSRange range = [str rangeOfString:@"_"];
        if (range.location + 1 < [str length]) {
            char c = [str characterAtIndex:range.location+1];
            [str replaceCharactersInRange:NSMakeRange(range.location, range.length+1) withString:[[NSString stringWithFormat:@"%c",c] uppercaseString]];
        }
    }
    return str;
}