Skip to content

Instantly share code, notes, and snippets.

@rbreve
Created November 21, 2012 05:40
Show Gist options
  • Save rbreve/4123266 to your computer and use it in GitHub Desktop.
Save rbreve/4123266 to your computer and use it in GitHub Desktop.
- (void)updateFromJson:(NSDictionary *)json
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HHmmssZ"];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"_(\\w)" options:0 error:NULL];
[json enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (obj == [NSNull null]) return;
if ([key isEqual:@"id"]) return;
NSString *_key = [key stringByReplacingOccurrencesOfString:@"class" withString:@"group"];
NSMutableString *newKey = [NSMutableString string];
__block NSInteger loc = 0;
__block NSInteger shorten = 0;
[regex enumerateMatchesInString:_key options:0 range:NSMakeRange(0, _key.length) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange r = [result rangeAtIndex:1];
[newKey appendString:[_key substringWithRange:NSMakeRange(loc, r.location - 1 - loc)]];
[newKey appendString:[[_key substringWithRange:r] uppercaseString]];
loc = r.location + 1;
shorten++;
}];
[newKey appendString:[_key substringWithRange:NSMakeRange(loc, _key.length - loc)]];
if ([newKey hasSuffix:@"At"]) {
obj = [formatter dateFromString:[obj stringByReplacingOccurrencesOfString:@":" withString:@""]];
}
if (![[self valueForKey:newKey] isEqual:obj]) {
[self setValue:obj forKey:newKey];
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment