From e9cae3271351bf23dbc7bd62bf70c5fd4902b81d Mon Sep 17 00:00:00 2001 From: Daisuke Murase Date: Fri, 27 Feb 2015 23:58:44 +0900 Subject: [PATCH] add swift support --- Realm+JSON/RLMObject+JSON.m | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Realm+JSON/RLMObject+JSON.m b/Realm+JSON/RLMObject+JSON.m index 1e12ed7..6c08c40 100644 --- a/Realm+JSON/RLMObject+JSON.m +++ b/Realm+JSON/RLMObject+JSON.m @@ -8,6 +8,12 @@ #import "RLMObject+JSON.h" +// RLMSchema private interface +@interface RLMSchema () +// class for string ++ (Class)classForString:(NSString *)className; +@end + #import static id MCValueFromInvocation(id object, SEL selector) { @@ -142,14 +148,13 @@ + (id)mc_createObjectFromJSONDictionary:(NSDictionary *)dictionary { NSMutableDictionary *result = [NSMutableDictionary dictionary]; NSDictionary *mapping = [[self class] mc_inboundMapping]; - Class modelClass = NSClassFromString([[self class] className]); for (NSString *dictionaryKeyPath in mapping) { NSString *objectKeyPath = mapping[dictionaryKeyPath]; id value = [dictionary valueForKeyPath:dictionaryKeyPath]; if (value) { - Class propertyClass = [modelClass mc_classForPropertyKey:objectKeyPath]; + Class propertyClass = [[self class] mc_classForPropertyKey:objectKeyPath]; if ([propertyClass isSubclassOfClass:[RLMObject class]]) { if (!value || [value isEqual:[NSNull null]]) { @@ -168,7 +173,7 @@ + (id)mc_createObjectFromJSONDictionary:(NSDictionary *)dictionary { } else if ([propertyClass isSubclassOfClass:[RLMArray class]]) { RLMProperty *property = [self mc_propertyForPropertyKey:objectKeyPath]; - Class elementClass = NSClassFromString(property.objectClassName); + Class elementClass = [RLMSchema classForString: property.objectClassName]; NSMutableArray *array = [NSMutableArray array]; for (id item in(NSArray*) value) { @@ -208,13 +213,12 @@ - (id)mc_createJSONDictionary { NSMutableDictionary *result = [NSMutableDictionary dictionary]; NSDictionary *mapping = [[self class] mc_outboundMapping]; - Class modelClass = NSClassFromString([[self class] className]); for (NSString *objectKeyPath in mapping) { NSString *dictionaryKeyPath = mapping[objectKeyPath]; id value = [self valueForKeyPath:objectKeyPath]; if (value) { - Class propertyClass = [modelClass mc_classForPropertyKey:objectKeyPath]; + Class propertyClass = [[self class] mc_classForPropertyKey:objectKeyPath]; if ([propertyClass isSubclassOfClass:[RLMObject class]]) { value = [value mc_createJSONDictionary]; @@ -227,7 +231,7 @@ - (id)mc_createJSONDictionary { value = [array copy]; } else { - NSValueTransformer *transformer = [modelClass mc_transformerForPropertyKey:objectKeyPath]; + NSValueTransformer *transformer = [[self class] mc_transformerForPropertyKey:objectKeyPath]; if (transformer) { value = [transformer reverseTransformedValue:value];