Ios RestKit-从RKRequestDescriptor中排除具有nil值的属性映射

Ios RestKit-从RKRequestDescriptor中排除具有nil值的属性映射,ios,objective-c,core-data,restkit,restkit-0.20,Ios,Objective C,Core Data,Restkit,Restkit 0.20,问题是,当值为nil时,我需要从有效负载中完全删除@“unitprice”属性,但如果请求中有值,则将其保留在那里。因此,OrderLine的有效负载将相应地如下所示:@{“id”:@“orderlineId”、@“unitprice”:@“unitprice”}或@{“id”:@“orderlineId”}请注意,映射是一对多关系。有可能这样做吗?非常感谢你的帮助,谢谢! /* 请求描述符 */ 设置在映射中将FaultValueFormissingAttribute分配给否。您不需要两种不同

问题是,当值为nil时,我需要从有效负载中完全删除@“unitprice”属性,但如果请求中有值,则将其保留在那里。因此,OrderLine的有效负载将相应地如下所示:@{“id”:@“orderlineId”、@“unitprice”:@“unitprice”}或@{“id”:@“orderlineId”}请注意,映射是一对多关系。有可能这样做吗?非常感谢你的帮助,谢谢! /* 请求描述符 */


设置
在映射中将FaultValueFormissingAttribute
分配给
。您不需要两种不同的映射(除非您确实希望在JSON中为某些属性包含
nil

似乎不可能实现具有1对多关系的动态请求描述符。因此,我必须通过检查nil值手动构建NSMutableDictionary,然后添加属性及其值,如下所示

[objectManager postObject:nil
                    path:@"/salesorder"
              parameters:[self customSalesOrderRequestMapping]
                 success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {... }

 - (NSMutableDictionary *) customSalesOrderRequestMapping {

    customSalesOrderAttributeDictionary = [NSMutableDictionary new];

    if (selectedSalesOrder.salesOrderId) [customSalesOrderAttributeDictionary addEntriesFromDictionary:@{@"id": selectedSalesOrder.salesOrderId}];

    if (selectedSalesOrder.salesOrderToOrderLines.count > 0) {

        NSMutableArray *orderLinesMutableArray = [NSMutableArray new];
         for (OrderLine *orderLine in selectedSalesOrder.salesOrderToOrderLines)
        {
            NSMutableDictionary *orderLineMutableDictionary = [NSMutableDictionary new];
            if (orderLine.orderlineId) [orderLineMutableDictionary addEntriesFromDictionary:@{@"id": orderLine.orderlineId}];
            if (orderLine.unitPrice) [orderLineMutableDictionary addEntriesFromDictionary:@{@"unitprice": orderLine.unitPrice}];
            [orderLinesMutableArray addObject:orderLineMutableDictionary];
    }

    [customSalesOrderAttributeDictionary addEntriesFromDictionary:@{@"lines": orderLinesMutableArray}];

  }
    return customSalesOrderAttributeDictionary;
}

你是说,
unitprice
是一对多的关系吗?显示OrderLine.h内容我已经更新了我的问题,因为assignsDefaultValueForMissingAttributes不会在JSON中排除“unitprice”属性(当它为nil时)并在它有值时包含它。问题在于我们的API,当“单价”没有更改时,我们应该将其从JSON映射中排除,因此我们将nil分配给“单价”,然后我们想为RestKit编写一些逻辑,从JSON映射中排除nil值。所以我认为这只有通过使用动态映射才能实现,对吗?感谢设置
assignsDefaultValueForMissingAttributes
to
NO
应该在映射结果中省略nil,您是否尝试过并看到其他结果?显示测试代码。我已经添加了这两行responseMapping.assignsDefaultValueForMissingAttributes=NO;orderLinesMapping.assignsDefaultValueForMissingAttributes=否;奇怪的是,没有运气,它确实排除了未映射的属性,但因为映射了“unitprice”,所以它将其保留为值nil。无论如何,谢谢。您发出了一个请求,因此您将其应用于所采取的反向映射。不幸的是,当值为nil时,我需要从负载中完全删除该属性,但如果该属性在请求中有值,则将其保留在那里。因此,有效负载将相应地如下所示:@{“id”:@“orderlineId”、@“unitprice”:@“unitprice”}或@{“id”:@“orderlineId”}请注意,映射是一对多关系。有可能这样做吗?非常感谢你的帮助,谢谢!
[objectManager postObject:nil
                    path:@"/salesorder"
              parameters:[self customSalesOrderRequestMapping]
                 success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {... }

 - (NSMutableDictionary *) customSalesOrderRequestMapping {

    customSalesOrderAttributeDictionary = [NSMutableDictionary new];

    if (selectedSalesOrder.salesOrderId) [customSalesOrderAttributeDictionary addEntriesFromDictionary:@{@"id": selectedSalesOrder.salesOrderId}];

    if (selectedSalesOrder.salesOrderToOrderLines.count > 0) {

        NSMutableArray *orderLinesMutableArray = [NSMutableArray new];
         for (OrderLine *orderLine in selectedSalesOrder.salesOrderToOrderLines)
        {
            NSMutableDictionary *orderLineMutableDictionary = [NSMutableDictionary new];
            if (orderLine.orderlineId) [orderLineMutableDictionary addEntriesFromDictionary:@{@"id": orderLine.orderlineId}];
            if (orderLine.unitPrice) [orderLineMutableDictionary addEntriesFromDictionary:@{@"unitprice": orderLine.unitPrice}];
            [orderLinesMutableArray addObject:orderLineMutableDictionary];
    }

    [customSalesOrderAttributeDictionary addEntriesFromDictionary:@{@"lines": orderLinesMutableArray}];

  }
    return customSalesOrderAttributeDictionary;
}