Created
July 25, 2014 15:47
-
-
Save MattFoley/cf49f4af2155aada6d52 to your computer and use it in GitHub Desktop.
Update CDM mapper to handle transformable core data attributes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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