Created
May 7, 2011 16:04
-
-
Save tlivings/960603 to your computer and use it in GitHub Desktop.
Quick and Dirty proxyForJson Implementation on NSManagedObject
This file contains hidden or 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
| @implementation NSManagedObject (NSManagedObject_JSON) | |
| /* | |
| * | |
| * This method is implemented to help NSObject+JSON with transforming to JSON as per SBJSON's JSONValue helper method. | |
| * | |
| */ | |
| -(NSDictionary*) proxyForJson { | |
| NSDictionary * attributes = [[self entity] attributesByName]; | |
| NSDictionary * relationships = [[self entity] relationshipsByName]; | |
| NSMutableDictionary * results = [NSMutableDictionary dictionary]; | |
| for (NSString * relationship in [relationships keyEnumerator]) { | |
| id value = [self valueForKey:relationship]; | |
| if(value) { | |
| if([value isKindOfClass:[NSSet class]]) { | |
| [results setObject:[(NSSet*)value allObjects] forKey:relationship]; | |
| } | |
| else { | |
| [results setObject:value forKey:relationship]; | |
| } | |
| } | |
| } | |
| for (NSString * attribute in [attributes keyEnumerator]) { | |
| id value = [self valueForKey:attribute]; | |
| if(value) { | |
| [results setObject:value forKey:attribute]; | |
| } | |
| } | |
| return results; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment