Skip to content

Commit

Permalink
Merge pull request #29 from typester/swift-support
Browse files Browse the repository at this point in the history
add swift support
  • Loading branch information
matthewcheok committed Mar 1, 2015
2 parents 7d6f6a2 + e9cae32 commit 951f6e0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Realm+JSON/RLMObject+JSON.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

#import "RLMObject+JSON.h"

// RLMSchema private interface
@interface RLMSchema ()
// class for string
+ (Class)classForString:(NSString *)className;
@end

#import <objc/runtime.h>

static id MCValueFromInvocation(id object, SEL selector) {
Expand Down Expand Up @@ -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]]) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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];
Expand All @@ -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];
Expand Down

0 comments on commit 951f6e0

Please sign in to comment.