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

Posted

技术标签:

【中文标题】Restkit 映射列表 - 没有响应描述符与加载的响应匹配【英文标题】:Restkit Mapping a lists - No response descriptors match the response loaded 【发布时间】:2016-05-20 18:59:23 【问题描述】:

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

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

    
  "productID" : 90,
  "productName" : "***lyn 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;


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

    `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 在复杂对象中映射字符串数组和自定义对象?

我用 代码 7.3 休息套件 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 这样的原语。

【问题讨论】:

你的响应描述符在哪里? @Wain 我更新了问题 【参考方案1】:

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

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

【讨论】:

以上是关于Restkit 映射列表 - 没有响应描述符与加载的响应匹配的主要内容,如果未能解决你的问题,请参考以下文章

用于发布 JSON 列表的 RestKit 映射

使用 RestKit 映射空响应的对象

没有映射的 RestKit HTTP 响应代码

RestKit 不喜欢为 POST 响应映射不同的对象

RestKit嵌套的一对多关系映射很慢

restkit,如何在没有对象映射的情况下访问对象