gpt4 book ai didi

ios - RestKit:POST 反序列化 - 预期类型 A 的对象映射,提供者为 B 返回一个

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:47:51 26 4
gpt4 key购买 nike

这是我的路由设置:

[manager.router.routeSet addRoute:[RKRoute routeWithClass:[UserProfile class]  pathPattern:@"/api/v1/users" method:RKRequestMethodPOST]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[UserProfileAndStatistics class] pathPattern:@"/api/v1/profile/:userId" method:RKRequestMethodGET]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[PrivateUserProfile class] pathPattern:@"/api/v1/privateprofile/" method:RKRequestMethodGET]];
[manager.router.routeSet addRoute:[RKRoute routeWithClass:[PrivateUserProfile class] pathPattern:@"/api/v1/privateprofile/" method:RKRequestMethodPOST]];

下面是我如何注册映射:

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping pathPattern:nil keyPath:nil statusCodes:nil];
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];

//the serialisation step
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[mapping inverseMapping] objectClass:[mapping objectClass] rootKeyPath:nil];
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];

最后我打电话:

[[RKObjectManager sharedManager] postObject:userProfile path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) { ... }

正确发布,我得到一个 HTTP 状态代码 200,数据发送到服务器。这意味着序列化步骤正在运行。当响应返回时,我得到一个错误:

Adding mapping error: Expected an object mapping for class of type 'PrivateUserProfile', provider returned one for 'UserProfileAndStatistics'

现在 PrivateUserProfileUserProfileAndStatistics 的映射非常相似

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[PrivateUserProfile class]];
[mapping addAttributeMappingsFromDictionary:@{
@"UserIdentifier" : @"userId",
@"UserName" : @"userName",
@"Name" : @"name",
@"Email" : @"emailAddress",
@"Bio" : @"bio",
@"Website" : @"webSite",
@"Avatar" : @"avatar"
}];

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[UserProfileAndStatistics class]];
[mapping addAttributeMappingsFromDictionary:@{
@"UserIdentifier" : @"userId",
@"UserName" : @"userName",
@"Name" : @"name",
@"Bio" : @"bio",
@"Website" : @"webSite",
@"Avatar" : @"avatar",
@"Posts" : @"posts",
@"Followers" : @"followers",
@"Following" : @"following"
}];

但为什么 RestKit 会选择一个而不是另一个呢?如何成功调试? RKRoute 和RKObjectMapping 之间有没有关系。 PrivateUserProfile 的映射用于序列化步骤,那么为什么它不用于对应的反序列化,我该如何使用它?

最佳答案

您在这里需要两个响应描述符 - 一个路径为 /api/v1/privateprofile/ 用于类 PrivateUserProfile,另一个路径为 /api/v1/profile/:userIdUserProfileAndStatistics:

[manager addResponseDescriptorsFromArray:@[

[RKResponseDescriptor responseDescriptorWithMapping:privateUserProfileMapping
pathPattern:@"/api/v1/privateprofile/"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)],
[RKResponseDescriptor responseDescriptorWithMapping:userProfileAndStatisticsMapping
pathPattern:@"/api/v1/profile/:userId"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]
]];

关于ios - RestKit:POST 反序列化 - 预期类型 A 的对象映射,提供者为 B 返回一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14205205/

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