在我的应用程序中,我执行以下操作:
[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;
}];