Restkit 映射列表 - 没有响应描述符与加载的响应匹配
Restkit Mapping a lists - No response descriptors match the response loaded
当我尝试映射包含字符串数组和自定义对象的复杂对象时遇到问题。我收到错误消息:没有响应描述符与加载的响应匹配。
我尝试将此 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;
}
但是当我请求对象时,映射失败并给我这个错误信息:>
`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
休息套件 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.[= 这样的原语。 15=]
更改您的路径模式以包含完整内容:
@"/thewebappv2/productController/productpage/:id"
当我尝试映射包含字符串数组和自定义对象的复杂对象时遇到问题。我收到错误消息:没有响应描述符与加载的响应匹配。
我尝试将此 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;
}
但是当我请求对象时,映射失败并给我这个错误信息:>
`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 休息套件 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.[= 这样的原语。 15=]
更改您的路径模式以包含完整内容:
@"/thewebappv2/productController/productpage/:id"