언제 우리의 서버에 성공적으로 사용자가 가입 한,이 같은 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>}
가 지금이 시점에서 붙어 :
그리고이 출력 결과 . message
뿐만 아니라 두 개의 개별 값으로 반환되는 code
에 액세스 할 수 있도록 서버의 오류 응답 키를 매핑하는 방법은 무엇입니까?
딱! 한 가지 변경을하고'for (TAG_RKErrorMessage * objectMapperErrorObjectsArray의 objectMapperErrorObject)'를 실행하면 errorCode에 액세스 할 수 있습니다. 도와 주셔서 감사합니다! –