我在这里做了什么?
我的类别会被下载并映射,
产品也会在日志记录中下载并映射,
但我的产品在每个类别下都是空的, 日Thnx!
{
"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个对象答案 0 :(得分:1)
假设您的ProductCategory类有
NSArray *Product
NSString * tit
创建顶级RKObjectMapping,例如
RKObjectMapping * ProductCategoryResponseMapping = [RKObjectMapping mappingForClass: [ProductCategoryResponse class]];
[ProductCategoryResponseMapping addAttributeMappingsFromDictionary:@{
@"productsCategories": @"productsCategories"
}];
创建新的类ProductCategoryResponse,它应该有
NSArray * productsCategories
将ProductCategoryResponseMapping用于响应描述符。
现在,您的回复将包含productsCategories数组,每个类别都包含Products和String tit。