2014-04-17 33 views
4

为简单起见,Rest API发送这个JSON response-body = {“status”:“ok”}。Restkit 0.22.0如何映射一个没有keyPath的简单JSON响应来发布

我建立我的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" 
} 

任何帮助,将不胜感激。

回答

3

你应该尝试keyPath到@“”和pathPattern为零,这里是我正在使用的一段代码,它对我来说完全正常工作。我正在做你完全一样的尝试。

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

延迟应答道歉。我会尝试你的建议。感谢分享。我会让你知道我的结果。 – user3337849

+0

谢谢。你的代码确实有效。再次感谢... – user3337849

0

因为您的响应描述符有pathPattern:@"status",所以只有在向服务器上的status路径发出请求时才会使用映射。这可能不是你想要的。您可能应该使用pathPattern:nil

+0

我试图用pathPattern:无成功。我需要知道如何为没有keyPath的简单JSON对象{“status”:“ok”}创建ObjectMapping。当我简单地尝试使用keyPath:@“status”时,我得到错误键值编码违规。我一直在阅读ObjectMapping上的RestKit git wiki,但他们没有展示如何为这个简单的情况。你有没有可以指点我的参考? – user3337849