当用户在我们的服务器上签约成功,它以200状态码和JSON有效载荷这样的回应:如何在Restkit中映射自定义错误键?
{
"error": null,
"result": {
"auth": {
"created_utc": 1420740197,
"device_token": "rQZJddrbD5tyEpznb8bVKeGlHqRNGyvOgDR;tQJBkpkfAXO6DQ4lNiG17lzu6IDc0hVBfR3RrN9o0txRQIYAa6fnf5d9LNaSRDMk9LrplgkITuMC37v;;;rvG35CJvV7dWZ5TQVYUWeHwAABvKvzTRpSDw5Qg9jQrmiUHLZptegFY=76421420740197"
},
"display_name": "a",
"email": "[email protected]",
"user_id": 7642,
"username": "a"
}
}
但如果[email protected]尝试重新注册时,其与响应400状态码和有效载荷JSON这样的:
{
"error": {
"code": 805,
"message": "there is another user with that username"
},
"result": null
}
我试图映射误差,这样,当Restkit返回一个错误,我得到的消息,以及该代码。这些都是我想做到这一点的方法:
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error.message" toKeyPath:@"errorMessage"]];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[self.objectManager addResponseDescriptor:errorDescriptor];
这其中显然只得到了错误的消息部分与该输出返回:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1004 "there is another user with that username" UserInfo=0x7feefcf8a730 {RKObjectMapperErrorObjectsKey=(
"there is another user with that username"
), NSLocalizedDescription=there is another user with that username}
于是我试图使RKErrorMessage的子类:
#import "RKErrorMessage.h"
@interface TAG_RKErrorMessage : RKErrorMessage
@property (strong, nonatomic) NSNumber *errorCode;
@end
,改变了映射到这一点:
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[TAG_RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error.message" toKeyPath:@"errorMessage"]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error.code" toKeyPath:@"errorCode"]];
并导致完全相同的输出:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1004 "there is another user with that username" UserInfo=0x7fa16c627ce0 {RKObjectMapperErrorObjectsKey=(
"there is another user with that username"
), NSLocalizedDescription=there is another user with that username}
所以最后我想这映射到至少尝试获得字典显示为的RKObjectMapperErrorObjectsKey
的NSError
的userInfo
:
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:@"error" toKeyPath:@"errorMessage"]];
而导致这种输出:
Error Domain=org.restkit.RestKit.ErrorDomain Code=1004 "<null>" UserInfo=0x7ff8abe678e0 {RKObjectMapperErrorObjectsKey=(
(null)
), NSLocalizedDescription=<null>}
现在我被困在这一点上。如何映射服务器错误响应的密钥,以便我可以访问返回的code
以及message
作为两个单独的值?
完美!要做一个改变,并为'(TAG_RKErrorMessage * objectMapperErrorObjectsArray中的objectMapperErrorObject)'执行''所以我可以访问errorCode。谢谢您的帮助! –