gpt4 book ai didi

iphone - RKObjectMapping 中的混淆设置映射 - Rest

转载 作者:行者123 更新时间:2023-12-03 19:51:53 26 4
gpt4 key购买 nike

请帮助使用 RestKit 进行集合映射,我不知道如何做到这一点。

我的 JSON 就像

{
"result":{
"success":"1",
"message":"You logged in successfully.",
"data":{
"user_id":"1",
"firstname":"somefirstname",
"lastname":"somelastname",
"username":"someusername",
"country_id":"someid",
"country_name":"somecountry",
"phone":"1234567890",
"status":"active"
}
}
}

到目前为止我所做的是

RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[Login class]];
[loginMapping addAttributeMappingsFromDictionary:@{ @"user_id":@"intUserID", @"firstname":@"strFirstName", @"lastname":@"strLastName"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
method:RKRequestMethodGET
pathPattern:@"PathTOLogin"
keyPath:@"result.data"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
NSURL *baseURL = [NSURL URLWithString:kAPIBaseURLString];
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
Login *loginObject = [[Login alloc] init];
[objectManager getObject:loginObject path:kUserLogin parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];

问题就像有时 JSON 响应可能是这样的

{
"result":{
"success":"0",
"message":"Invalid Username or Password",
}
}

所以它会抛出错误

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001

因此,我想在模型类中添加 2 个属性作为成功/消息以及所有其他属性,但我坚持设置自定义映射,因为这些属性位于根目录上,其他属性位于关键路径“result.data”中。

有人可以帮忙吗?

最佳答案

您应该将登录映射更改为:

RKObjectMapping *loginMapping = [RKObjectMapping mappingForClass:[Login class]];
[loginMapping addAttributeMappingsFromDictionary:@{ @"success":@"success", @"message":@"message", @"data.user_id":@"intUserID", @"data.firstname":@"strFirstName", @"data.lastname":@"strLastName"}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping
method:RKRequestMethodGET
pathPattern:@"PathTOLogin"
keyPath:@"result"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

基本上,从响应描述符的关键路径中删除“数据”,并在映射关键路径中使用它。

关于iphone - RKObjectMapping 中的混淆设置映射 - Rest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18890105/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com