Restkit映射列表 - 没有响应描述符与加载的响应匹配

时间:2016-05-20 18:59:23

标签: objective-c iphone restkit restkit-0.20

当我尝试映射包含字符串数组和自定义对象的复杂对象时,我遇到了问题。我收到错误:没有响应描述符匹配加载的响应。

我尝试将此JSON映射到复杂的对象:

    {
  "productID" : 90,
  "productName" : "brooklyn decker",
  "brandID" : 58,
  "productPictureURLs" : null,
  "colors" : [ ],
  "sizes" : [ ],
  "configurationsAvailable" : [ {
    "size" : "m",
    "color" : "red",
    "price" : 100.00,
    "onSalePrice" : null,
    "onSale" : false,
    "favorited" : false,
    "quantityInShoppingCart" : 0,
    "productPictureURLs" : null,
    "productVariantId" : 1171
  }, {
    "size" : "b",
    "color" : "blue",
    "price" : 100.00,
    "onSalePrice" : null,
    "onSale" : false,
    "favorited" : false,
    "quantityInShoppingCart" : 0,
    "productPictureURLs" : null,
    "productVariantId" : 1173
  }, {
    "size" : "b",
    "color" : "grey",
    "price" : 100.00,
    "onSalePrice" : null,
    "onSale" : false,
    "favorited" : false,
    "quantityInShoppingCart" : 0,
    "productPictureURLs" : null,
    "productVariantId" : 1174
  } ],
  "descriptionBody" : "description"
}

我使用这些类:` @interface TransferSizeAndColorConfiguration:NSObject

    @property (nonatomic) int64_t * productVariantId;

    @property (nonatomic, strong) NSString * color;
    @property (nonatomic, strong) NSString * size;

    @property (nonatomic, strong) NSDecimalNumber * price;
    @property (nonatomic, strong) NSDecimalNumber * onSalePrice;
    @property (nonatomic) BOOL * onSale;
    @property (nonatomic) BOOL * favorited;
    @property (nonatomic) int * quantityInShoppingCart;
    @property (nonatomic, strong) NSArray <TransferObjectString *> *productPictureURLs;

`
and for the product:

    @property (nonatomic) int64_t productID;
@property (nonatomic) int64_t brandID;
@property (nonatomic) NSArray <TransferObjectString *> * colors;
@property (nonatomic) NSArray <TransferObjectString *> * sizes;
@property (nonatomic) NSArray <TransferObjectString *> * productPictureURLs;
@property (nonatomic) NSArray <TransferSizeAndColorConfiguration *> * configurationsAvailable; // Class not yet implemented
@property (nonatomic) NSString * productName;
@property (nonatomic) NSString * descriptionBody;;

我使用这里映射它们:

        // You need a mapping for the cofiguration of size and color as well and you have to set up a relation between the configuration and the product
    RKObjectMapping *configurationForSizeAndColorMapping = [RKObjectMapping mappingForClass:[TransferSizeAndColorConfiguration  class]];
    [configurationForSizeAndColorMapping addAttributeMappingsFromArray:@[@"productVariantId",  @"color",  @"size",  @"price", @"onSalePrice", @"onSale", @"favorited", @"quantityInShoppingCart"]];


    // The prduct picture urls are stored in an array and therefore need to have a mapping to other objects that hold only the urls
    // Map multiple objects to one keypath
    RKObjectMapping *productPictureStringMapping = [RKObjectMapping mappingForClass:[TransferObjectString class]];
    [productPictureStringMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"string"]];

    [configurationForSizeAndColorMapping addRelationshipMappingWithSourceKeyPath:@"productPictureURLs" mapping:productPictureStringMapping];


    RKObjectMapping *getProductPageMapping = [RKObjectMapping mappingForClass:[TransferProductPage class]];
    [getProductPageMapping addAttributeMappingsFromArray:@[@"productID", @"brandID", @"productName", @"descriptionBody"]];


    RKObjectMapping *stringMapping = [RKObjectMapping mappingForClass:[TransferObjectString class]];
    [stringMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"string"]];
    [getProductPageMapping addRelationshipMappingWithSourceKeyPath:@"colors" mapping:stringMapping];
    [getProductPageMapping addRelationshipMappingWithSourceKeyPath:@"sizes" mapping:stringMapping];
    [getProductPageMapping addRelationshipMappingWithSourceKeyPath:@"productPictureURLs" mapping:stringMapping];
    [getProductPageMapping addRelationshipMappingWithSourceKeyPath:@"configurationsAvailable" mapping:configurationForSizeAndColorMapping];



    return getProductPageMapping;

}

但是当我请求对象时,映射失败并给我这个错误消息:&gt;

    `No mappable object representations were found at the key paths searched."`  UserInfo={NSLocalizedDescription=No mappable object representations were found at the key paths searched., NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: 
    The representation inputted to the mapper was found to contain nested object representations at the following key paths: brandID, colors, configurationsAvailable, descriptionBody, productID, productName, productPictureURLs, sizes
    This likely indicates that you have misconfigured the key paths for your mappings., keyPath=null

`

我的问题是我不知道映射字符串数组和配置数组的最佳方法。 我知道您可以通过AFNetworking将JSON反序列化为数组。但在这种情况下,就我所见,这会导致多个请求,我不知道如何映射这些请求。

我的工作是创建一个只存储字符串的自定义对象,而不是使用nil键路径将多个值映射到键。

如何使用Restkit在复杂对象中映射字符串和自定义对象的数组?

我用 xcode 7.3 restkit 0.26

编辑: 这是我创建描述符并发送请求的地方:

     NSURL *baseURL = [NSURL URLWithString:@"http://example.ngrok.io"];
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];

// Initialize the object manager
    RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];


    // Descriptor
    RKResponseDescriptor* getProductPageResponse =
    [RKResponseDescriptor responseDescriptorWithMapping:[self getProductMapping]
                                                 method:RKRequestMethodGET
                                            pathPattern:@"/thewebappv2/productController/productpage"
                                                keyPath:nil
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


    [objectManager addResponseDescriptor:getProductPageResponse];





    int64_t prodcutID = 90;

    [objectManager getObjectsAtPath:[NSString stringWithFormat:@"/thewebappv2/productController/productpage/%lld", prodcutID]
                                           parameters:nil
                                              success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

                                                  // Assign the values you get from the TransferProductpage to the Product property
                                                  // the Strings are wrapped in custom Objects so they can be deserialized using Restkit



                                              }
                                              failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                  NSLog(@"The Prodcut could not be downlaoded:': %@", error.localizedDescription);


                                              }];

更新:除了@wain的修复之外,您还必须将映射的类的类型更改为对象(例如NSNumber),而不是像int_64那样的原语。

1 个答案:

答案 0 :(得分:0)

更改路径模式以包含完整内容:

@"/thewebappv2/productController/productpage/:id"