Restkit 0.22.0如何映射一个简单的JSON响应,没有keyPath发布

时间:2014-04-17 00:12:25

标签: json restkit response restkit-0.20 rkmappingtest

为简单起见,Rest API发送此JSON response-body = {“status”:“ok”}。

我设置了我的Restkit映射,例如......创建了一个名为StatusResponse的类,其中有一个@property (nonatomic, assign) NSString *status;

RKObjectMapping *statusResponseMapping = [RKObjectMapping mappingForClass:[StatusResponse class]];
    [statusResponseMapping addAttributeMappingsFromDictionary:@{@"status":@"status"}]; 

// also tried : [statusResponseMapping addAttributeMappingsFromDictionary:@[@"status"]]; which resulted in same error

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

RKResponseDescriptor *statusResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:statusResponseMapping method:RKRequestMethodPOST pathPattern:@"status" keyPath:nil statusCodes:statusCodes];

[[RKObjectManager sharedManager] addResponseDescriptor:statusResponseDescriptor];

I'm running a post which is successful, but I get this error back:

NSUnderlyingError=0x17024d440 "No mappable object representations were found at the key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.}
response.body={
  "status": "ok"
}

对此有任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

你应该尝试使用keyPath to @""和pathPattern为零,这里是我使用的代码片段,对我来说完全正常。我正在做你想做的事情。

RKResponseDescriptor *statusResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[ResponseStatus rkObjectMappingForResponse:YES] method:RKRequestMethodAny pathPattern:nil keyPath:@"" statusCodes:statusCodes];

答案 1 :(得分:0)

由于您的响应描述符具有pathPattern:@"status",因此只有在向服务器上的status路径发出请求时才会使用映射。那可能不是你想要的。

您应该使用pathPattern:nil