gpt4 book ai didi

ios - restkit 映射嵌套数组为空(带有 json 响应的嵌套 restkit 请求)

转载 作者:行者123 更新时间:2023-11-29 12:34:05 27 4
gpt4 key购买 nike

我在这里做的事情很奇怪吗?

我的类别得到下载和映射,

产品也像日志记录所说的那样被下载和映射,

但是我的产品在每个分类下都是空的,谢谢!

{
"productsCategories" : [
{
"product" : [
{
"price" : 3.99,
"title" : "Product A"
},
{
"price" : 3.99,
"title" : "ProductB "
}
],
"categoryTitle" : “a category“
}
]
}




RKObjectMapping *productCategoryMapping = [RKObjectMapping mappingForClass:[ProductCategory class]];
[productCategoryMapping addAttributeMappingsFromDictionary:@{
@"categoryTitle": @"tit"
}];

RKObjectMapping* productMapping = [RKObjectMapping mappingForClass:[Product class] ];
[productMapping addAttributeMappingsFromDictionary:@{
@"title": @"tit",
@"price": @"prc"
}];
[productCategoryMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"product"
toKeyPath:@"product"
withMapping:productMapping]];


RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:productCategoryMapping method:RKRequestMethodPOST pathPattern:@"productByCategory" keyPath:@"productsCategories" statusCodes:[NSIndexSet indexSetWithIndex:200]];

NSURL *url=[NSURL URLWithString:@"http://www.example.com/REST/v2/"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:url];
[manager addResponseDescriptor:responseDescriptor];

NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];
[mutableParameters setValue:@"1" forKey:@"id"];
[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:productCategoryMapping method:RKRequestMethodPOST pathPattern:@"productByCategory" keyPath:@"productsCategories" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

[manager postObject:request path:@"productByCategory" parameters:mutableParameters success:^(RKObjectRequestOperation *operation, RKMappingResult *result){

self.productCategoryArr=result.array;
[self.tblDetails reloadData];

}failure:^(RKObjectRequestOperation *operation, NSError *error) {

RKLogError(@"Operation failed with error: %@", error);
self.productCategoryArr=nil;

}];

日志显示正在为每个产品映射对象,但我只得到

ProductCategory: 0x7bf53510
ProductCategory: 0x7be57f00

数组和每个中的 0 个对象

最佳答案

假设您的 ProductCategory 类有

NSArray *Product
NSString * tit

创建一个顶层 RKObjectMapping,例如,

RKObjectMapping * ProductCategoryResponseMapping = [RKObjectMapping mappingForClass:  [ProductCategoryResponse class]];

[ProductCategoryResponseMapping addAttributeMappingsFromDictionary:@{
@"productsCategories": @"productsCategories"
}];

并创建应该有的新类 ProductCategoryResponse

NSArray * productsCategories

将 ProductCategoryResponseMapping 用于响应描述符。

现在您的响应将包含一系列 productsCategories,每个包含一系列 Products 和 String tit。

关于ios - restkit 映射嵌套数组为空(带有 json 响应的嵌套 restkit 请求),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26834735/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com