【发布时间】:2026-01-22 18:55:01
【问题描述】:
我在这里做了什么奇怪的事情吗?
我的类别被下载和映射,
产品也会按照日志记录的方式被下载和映射,
但我的产品在每个类别下都是空的, 谢谢!
{
"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 个对象
【问题讨论】:
-
您需要提供更多信息。将
description方法实现添加到ProductCategory和Product,以便日志告诉您它们包含的内容。打开跟踪日志并提供一些详细信息。展示您如何测试数组是否为空,并展示准确的 JSON,它会产生您描述的 2 个ProductCategory实例的结果。
标签: ios restkit rkobjectmapping