No response descriptors match the response loaded
I have problems when I try to map a complex object that contains arrays of strings and custom objects. I get the error: No response descriptors match the response loaded.
I try to map this JSON to a complex Object:
{
"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"
}
I use these classes:` @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;;
I map them using here:
// 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;
}
But when I request the object the mapping fails and gives me this error message: >
`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
`
My problem is that I do not know the best way to map the arrays of strings and the array of configuration. I know that you can AFNetworking to deserialize JSON into arrays. But in this case this would result in multiple requests as far as I can see and I don't know how to map those.
My work around was to make a custom object that only stores a string and than use the nil key path to map multiple values to the keys.
How do you map arrays of strings and custom objects in a complex object with Restkit?
I use xcode 7.3 restkit 0.26
Edit: This is where I create the descriptor and send the request:
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);
}];
Update: In addition to the fix by @wain you have to change the types of the classes you map to to be objects (eg NSNumber) and not primitives like int_64.
更改路径模式以包含完整内容:
@"/thewebappv2/productController/productpage/:id"
链接地址: http://www.djcxy.com/p/49204.html
上一篇: [RKObjectMapping addPropertyMapping:]在ios中崩溃
下一篇: 没有响应描述符匹配加载的响应