Ios RESTKIT仅响应映射结果字典或数组中的第一个KVC

Ios RESTKIT仅响应映射结果字典或数组中的第一个KVC,ios,arrays,restkit,response,partial,Ios,Arrays,Restkit,Response,Partial,我是新来的这将是我的第一个问题, 任何提示都将不胜感激 我从restkit请求中收到一个字典作为响应,但只包含一个值,我的json中缺少了一堆KV 我做错了吗 API的回应: {"categories" : [ { "status" : 1, "rest_id" : 1, "id" : 1, "title" : "01. BasicC", "description" : "basic description" }, { "status" : 1, "rest_id"

我是新来的这将是我的第一个问题, 任何提示都将不胜感激

我从restkit请求中收到一个字典作为响应,但只包含一个值,我的json中缺少了一堆KV

我做错了吗

API的回应:

{"categories" : [
{
  "status" : 1,
  "rest_id" : 1,
  "id" : 1,
  "title" : "01. BasicC",
  "description" : "basic description"
},
{
  "status" : 1,
  "rest_id" : 1,
  "id" : 26,
  "title" : "01. Deli",
  "description" : "deli description"
}
]}
要请求的IOS函数:

- (void)loadProduct{
_categoriesDic=[[NSDictionary alloc]init];
RKObjectMapping* productMapping = [RKObjectMapping mappingForClass:[ProductCategory     class]];
[productMapping addAttributeMappingsFromDictionary:@{
                                                 @"id": @"id",
                                                 @"rest_id": @"rest_id",
                                                 @"title": @"title",
                                                 @"description": @"description",
                                                 @"status":@"status"
                                                 }];
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:productMapping method:RKRequestMethodPOST pathPattern:nil keyPath:@"categories" statusCodes:[NSIndexSet indexSetWithIndex:200]];

RKObjectManager *objectManager = [[RKObjectManager alloc] init];
[objectManager addResponseDescriptor:responseDescriptor];

NSString *jsonRequest = [NSString stringWithFormat:@"id=1"];
NSURL *url=[NSURL URLWithString:@"http://example.com/REST/v2/productCategories"];
NSData *json_data = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: json_data];

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    RKLogInfo(@"Load collection of Categories: %@", mappingResult.dictionary);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
}
答复:

Load collection of Categories: {
categories =     (
    "basic description",
    "deli description"
);
}

虽然您的代码做了一些稍微奇怪的事情,比如创建一个空的不可变字典,并将其发布到应该使用GET的端点,但它似乎确实起作用了。看起来您只是没有完全理解响应,这是因为您无意中重写了一个内置方法

您显示的日志包含:

{
    categories =     (
        "basic description",
        "deli description"
    );
}
是包含1个键类别的字典的描述,这是一个包含2个对象的数组。现在,这两个对象的描述方法也被调用,以便生成日志内容。不幸的是,您有一个名为description的属性,因此可以访问该属性而不是超类实现,因此您只能获得描述

现在,这并不意味着映射不起作用,只是日志有误导性


您应该将目标对象上的描述更改为另一个名称,如overview,这样您的日志将有意义,以后您将不会看到类似的问题。

Thnx Wain,这确实帮助了我,所以您是说我应该使用GET而不是POST从API检索数据?我认为在帖子正文中设置参数而不是使用参数可能是个好主意?=在url中,你有一个很好的例子来说明我的情况吗?再次强调一下,我的意思是,当你调用…/v2/productCategories时,我希望你能做一个GET来收集数据和一个POST来更新现有的产品类别。实际上,我希望端点是…/v2/productCategory。它不一定是这样的,这只是一个问题,如何休息和接近'规格',你正试图成为。你可能会对我感兴趣