我在这里有点新意,这将是我的第一个问题, 任何提示将不胜感激。
我从我的restkit请求收到一个字典作为响应,但只包含一个值,我从我的json中缺少一堆KV,
我做错了吗?Jason对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"
);
}
答案 0 :(得分:0)
虽然你的代码做了一些有点奇怪的事情(比如创建一个空的不可变字典,以及POST到应该使用GET的端点),但它看起来确实有效。看起来你还没有完全理解响应,那是因为你无意中覆盖了内置方法。
您显示的日志包含:
{
categories = (
"basic description",
"deli description"
);
}
是包含1个键(类别)的字典的description
,它是一个包含2个对象的数组。现在,这两个对象也调用了description
方法,以便生成日志内容。不幸的是,你有一个名为description
的属性,因此可以访问而不是超类实现。因此你只需要获得描述。
现在,这并不意味着映射无效,只是日志有误导性。
您应该将目标对象上的description
更改为其他名称,例如overview
,然后您的日志将会有意义(将来您不会看到类似的问题)。