Created
October 9, 2012 15:08
-
-
Save lukeredpath/3859420 to your computer and use it in GitHub Desktop.
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
{ | |
"user": { | |
"id": 999, | |
"name": "Joe Bloggs" | |
}, | |
"stream": [ | |
{ | |
"link": { | |
"url": "http://www.example.com", | |
"id": 123 | |
} | |
}, | |
{ | |
"status": { | |
"text": "Hellow", | |
"id": 123 | |
} | |
}, | |
{ | |
"photo": { | |
"url": "http://www.example.com/photo.jpg", | |
"id": 123 | |
} | |
} | |
] | |
} |
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 SomeClassRepresentingJustThisResponse | |
// has properties for user, streamItems; | |
+ (RKObjectMapping *)objectMapping | |
{ | |
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:self]; | |
// this one works | |
RKRelationshipMapping *eventMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:[User objectMapping]]; | |
[mapping addPropertyMapping:eventMapping]; | |
/* one mapping for each type of supported stream item */ | |
NSDictionary *streamMappings = @{ | |
@"image": [Image objectMapping], | |
@"link": [Link objectMapping], | |
@"status": [Status objectMapping] | |
}; | |
RKDynamicMapping *dynamicPrerollItemMapping = [[RKDynamicMapping alloc] init]; | |
[dynamicPrerollItemMapping setObjectMappingForDataBlock:^RKObjectMapping *(NSDictionary *data) { | |
NSString *type = [[data allKeys] objectAtIndex:0]; | |
return [prerollMappings objectForKey:type]; | |
}]; | |
// this one gives me a collection of the right objects, but no mapped attributes | |
RKRelationshipMapping *prerollItemsMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"stream" toKeyPath:@"streamItems" withMapping:dynamicPrerollItemMapping]; | |
[mapping addPropertyMapping:prerollItemsMapping]; | |
return mapping; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment