Iphone RestKit 0.2x:如何在应用映射之前检查键路径的存在
在我的应用程序中,我执行以下操作:Iphone RestKit 0.2x:如何在应用映射之前检查键路径的存在,iphone,objective-c,cocoa-touch,restkit-0.20,Iphone,Objective C,Cocoa Touch,Restkit 0.20,在我的应用程序中,我执行以下操作: [itineraryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"inboundInfo" toKeyPath:@"inboundInfo" withMapping:flightInfoMapping]]; //configuring the dynamic mapping [dynamicMapping setObjectMappingF
[itineraryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"inboundInfo" toKeyPath:@"inboundInfo" withMapping:flightInfoMapping]];
//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;
}];
但是键inboundInfo
可能会返回,也可能不会返回JSON
,根据一些标准,我不想为objectManager
添加一个全新的(大的)响应描述符来满足这种情况,在添加属性映射之前,是否有方法检查inboundInfo
键路径是否存在
p、 如果inboundInfo
没有带JSON
返回,那么上面的代码行将导致崩溃,删除应用程序中的代码行就可以了
编辑:使用RKDynamicMapping
解决,如下所示:
[itineraryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"inboundInfo" toKeyPath:@"inboundInfo" withMapping:flightInfoMapping]];
//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;
}];
您可以使用
RKDynamicMapping
运行一个代码块,该代码块分析响应并决定要应用哪个映射
(动态对象映射部分)方法setObjectMapping:whenValueOfKeyPath:isEqualTo在文档中已过时,因为它在0.2x中不存在。。。