为简单起见,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"
}
对此的任何帮助将不胜感激。
最佳答案
您应该尝试将 keyPath 设为 @""并将 pathPattern 设为 nil,这是我正在使用的一段代码,它对我来说非常适合。我正在做与您正在尝试做的完全相同的事情。
RKResponseDescriptor *statusResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[ResponseStatus rkObjectMappingForResponse:YES] method:RKRequestMethodAny pathPattern:nil keyPath:@"" statusCodes:statusCodes];
关于json - Restkit 0.22.0 如何映射没有要发布的 keyPath 的简单 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23122430/