Skip to content

Commit

Permalink
Merge pull request #23 from raspu/master
Browse files Browse the repository at this point in the history
Added deepShallowCopy
  • Loading branch information
matthewcheok committed Jan 9, 2015
2 parents 1b951aa + 7a53983 commit 3c60ec5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions Realm+JSON/RLMObject+Copying.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@interface RLMObject (Copying)

- (instancetype)shallowCopy;
- (instancetype)deepCopy;
- (void)mergePropertiesFromObject:(id)object;

@end
31 changes: 31 additions & 0 deletions Realm+JSON/RLMObject+Copying.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,35 @@ - (void)mergePropertiesFromObject:(id)object {
}
}

- (instancetype)deepCopy {
Class class = NSClassFromString([[self class] className]);

RLMObject *object = [[class alloc] init];

for (RLMProperty *property in self.objectSchema.properties) {

if (property.type == RLMPropertyTypeArray) {
RLMArray *thisArray = [self valueForKeyPath:property.name];
RLMArray *newArray = [object valueForKeyPath:property.name];

for (RLMObject *currentObject in thisArray) {
[newArray addObject:[currentObject deepCopy]];
}

}
else if (property.type == RLMPropertyTypeObject) {
RLMObject *value = [self valueForKeyPath:property.name];
[object setValue:[value deepCopy] forKeyPath:property.name];
}
else {
id value = [self valueForKeyPath:property.name];
[object setValue:value forKeyPath:property.name];
}
}

return object;
}



@end

0 comments on commit 3c60ec5

Please sign in to comment.