Skip to content

Instantly share code, notes, and snippets.

@sbrocket
Created May 10, 2011 20:59
Show Gist options
  • Select an option

  • Save sbrocket/965374 to your computer and use it in GitHub Desktop.

Select an option

Save sbrocket/965374 to your computer and use it in GitHub Desktop.
+ (void)initialize {
if (self == [AppDataBase class]) {
void (^dateAccessorFactory)(NSString*,NSString*) = ^(NSString *key, NSString *strKey) {
void(^setterBlock)(id,NSDate*) = [^void(id self, NSDate *val) {
[self willChangeValueForKey:key];
[self setPrimitiveValue:val forKey:key];
[self setPrimitiveValue:nil forKey:strKey];
[self didChangeValueForKey:key];
} copy];
NSString*(^getterBlock)(id) = [^NSString*(id self) {
NSString *strVal = [self primitiveValueForKey:strKey];
if (strVal == nil) {
strVal = [self stringByFormattingDate:[self valueForKey:key]];
[self setPrimitiveValue:strVal forKey:strKey];
}
return strVal;
} copy];
NSString *setterName = [NSString stringWithFormat:@"set%@%@:", [[key substringToIndex:1] uppercaseString], [key substringFromIndex:1]];
class_replaceMethod(self, NSSelectorFromString(setterName), pl_imp_implementationWithBlock(setterBlock), "v@:@");
class_replaceMethod(self, NSSelectorFromString(strKey), pl_imp_implementationWithBlock(getterBlock), "@@:");
};
dateAccessorFactory(@"releaseDate", @"releaseDateString");
dateAccessorFactory(@"createdDate", @"createdDateString");
dateAccessorFactory(@"updatedDate", @"updatedDateString");
dateAccessorFactory(@"changeDate", @"changeDateString");
dateAccessorFactory(@"jsonCreatedDate", @"jsonCreatedString");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment