ios - RestKit - 从 RKRequestDescriptor 中排除具有 nil 值的属性映射
<p><p>问题是当值为 nil 时,我需要从有效负载中完全删除属性 @"unitprice",但如果它在请求中有值,则将其保留在那里。因此,OrderLine 的有效负载应如下所示:@{"id":@"orderlineId", @"unitprice":@"unitprice"} OR @{"id":@"orderlineId"} 请注意,映射是一对多的关系。是否有可能做到这一点?非常感谢您的帮助,谢谢!
/*
请求描述符
*/</p>
<pre><code>+(RKObjectMapping*) getSalesOrderMapping:(RKRequestMethod)method {
RKEntityMapping *requestMapping = [RKEntityMapping mappingForEntityForName:@"SalesOrder"
inManagedObjectStore:];
RKEntityMapping *orderLinesMapping = [RKEntityMapping mappingForEntityForName:@"OrderLine"
inManagedObjectStore:];
requestMapping.identificationAttributes = @[ @"salesOrderId" ];
NSMutableDictionary *attributeMappings = [NSMutableDictionary dictionaryWithDictionary:@{
@"id": @"salesOrderId",
}];
NSDictionary *orderLineAttributeMappings = @{
@"id": @"orderlineId",
@"unitprice": @"unitPrice"
};
;
[requestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"lines"
toKeyPath:@"salesOrderToOrderLines"
withMapping:orderLinesMapping]];
returnrequestMapping;
}
</code></pre>
<p> <img src="/image/BLKgW.png" alt="enter image description here"/> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>在映射上将 <code>assignsDefaultValueForMissingAttributes</code> 设置为 <code>NO</code>。您不需要 2 个不同的映射(除非您确实想在 JSON 中包含 <code>nil</code> 的某些属性)。</p></p>
<p style="font-size: 20px;">关于ios - RestKit - 从 RKRequestDescriptor 中排除具有 nil 值的属性映射,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/24443606/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/24443606/
</a>
</p>
页:
[1]