1

在我的应用程序中,我执行以下操作:

[itineraryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"inboundInfo" toKeyPath:@"inboundInfo" withMapping:flightInfoMapping]];

但是根据某些标准,密钥inboundInfo可能会也可能不会返回JSON,我不想添加一个全新的(大)响应描述符来objectManager满足这种情况,但是,有没有办法检查inboundInfo密钥路径是否存在还是在添加属性映射之前?

ps 万一inboundInfo没有返回JSON上面的行会导致崩溃,删除应用程序的行就可以了。

编辑:使用RKDynamicMapping这样解决:

//configuring the dynamic mapping
[dynamicMapping setObjectMappingForRepresentationBlock:^RKEntityMapping *(id representation) {

    if([[representation objectForKey:@"inboundInfo"] isKindOfClass:[NSDictionary class]]) {
        if(![itineraryMapping.propertyMappings containsObject:inboundInfoMapping]) { //to prevent adding inboundInfoMapping more than once
            [itineraryMapping addPropertyMapping:inboundInfoMapping];
        }
    }

    //if inboundInfo is not a dictionary simply return the itineraryMapping without adding inboundInfoMapping on it
    return itineraryMapping;

}];
4

1 回答 1

2

您可以使用RKDynamicMapping运行一段代码来分析响应并决定您希望应用哪个映射。

http://restkit.org/api/latest/Classes/RKDynamicMapping.html

(动态对象映射部分)https://github.com/RestKit/RestKit/wiki/Object-mapping

于 2013-06-30T07:28:27.133 回答