RestKit: Dynamic Nested Attributes with an Array

浪尽此生 提交于 2019-12-06 03:48:58

I'd say your best bet is to create a response descriptor with a key path of @"results" and a dynamic mapping. That mapping would return a mapping which maps into an NSDictionary and which has a number of relationships defined. This dictionary is just a container to facilitate the other mappings (the relationships).

The relationships are created by iterating the keys of the representation provided to the dynamic mapping and creating one relationship per iteration, using the testMapping, but without the addAttributeMappingFromKeyOfRepresentationToAttribute as you can now use direct attribute access (and the inner type attribute).

Using setObjectMappingForRepresentationBlock:, your block is provided with the representation, which in your case is an NSDictionary of the deserialised JSON. Inside the block you create a mapping just as you usually would, but with the contents based on the keys in the dictionary.


RKObjectMapping *testMapping = [RKObjectMapping mappingForClass:[Test class]];
[testMapping addAttributeMappingsFromDictionary:@{ @"id": @"testId", @"name": @"testName" }];

[dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
    RKObjectMapping *testListMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
    for (NSString *key in representation) {
        [testListMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeypath:key toKeyPath:key withMapping:testMapping];
    }

    return testListMapping;
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!