Skip to content

Instantly share code, notes, and snippets.

@MattFoley
Created July 25, 2014 15:47
Show Gist options
  • Save MattFoley/cf49f4af2155aada6d52 to your computer and use it in GitHub Desktop.
Save MattFoley/cf49f4af2155aada6d52 to your computer and use it in GitHub Desktop.
Update CDM mapper to handle transformable core data attributes.
- (Class)expectedClassForObject:(NSManagedObject *)object andKey:(id)key
{
NSDictionary *attributes = [[object entity] attributesByName];
NSAttributeDescription *attributeDescription = [attributes valueForKey:key];
NSString *className = [attributeDescription attributeValueClassName];
if (!className) {
const char *className = [[object.entity managedObjectClassName] cStringUsingEncoding:NSUTF8StringEncoding];
const char *propertyName = [key cStringUsingEncoding:NSUTF8StringEncoding];
Class managedObjectClass = objc_getClass(className);
objc_property_t prop = class_getProperty(managedObjectClass, propertyName);
NSString *attributeString = [NSString stringWithCString:property_getAttributes(prop) encoding:NSUTF8StringEncoding];
const char *destinationClassName = [[self propertyTypeFromAttributeString:attributeString] cStringUsingEncoding:NSUTF8StringEncoding];
return objc_getClass(destinationClassName);
} else {
return NSClassFromString(className);
}
}
- (NSString *)propertyTypeFromAttributeString:(NSString *)attributeString
{
NSString *type = [NSString string];
NSScanner *typeScanner = [NSScanner scannerWithString:attributeString];
[typeScanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"@"] intoString:NULL];
if ([typeScanner isAtEnd]) {
return @"NULL";
} else {
[typeScanner scanCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"\"@"] intoString:NULL];
[typeScanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"\""] intoString:&type];
return type;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment