ios - RestKit:POST 反序列化 - 期望 A 类的对象映射,提供者为 B 返回一个
<p><p>这是我的路由设置:</p>
<pre><code>pathPattern:@"/api/v1/users" method:RKRequestMethodPOST]];
pathPattern:@"/api/v1/profile/:userId" method:RKRequestMethodGET]];
pathPattern:@"/api/v1/privateprofile/" method:RKRequestMethodGET]];
pathPattern:@"/api/v1/privateprofile/" method:RKRequestMethodPOST]];
</code></pre>
<p>这是我注册映射的方式:</p>
<pre><code> RKResponseDescriptor *responseDescriptor = ;
[ addResponseDescriptor:responseDescriptor];
//the serialisation step
RKRequestDescriptor *requestDescriptor = objectClass: rootKeyPath:nil];
[ addRequestDescriptor:requestDescriptor];
</code></pre>
<p>最后我打电话:</p>
<pre><code>[ postObject:userProfile path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) { ... }
</code></pre>
<p>正确发布后,我得到一个 HTTP 状态代码 200,并且数据进入服务器。这意味着序列化步骤正在工作。当响应回来时,我得到一个错误:</p>
<pre><code>Adding mapping error: Expected an object mapping for class of type 'PrivateUserProfile', provider returned one for 'UserProfileAndStatistics'
</code></pre>
<p>现在 <code>PrivateUserProfile</code> 和 <code>UserProfileAndStatistics</code> 的映射非常相似</p>
<pre><code>RKObjectMapping *mapping = ];
[mapping addAttributeMappingsFromDictionary:@{
@"UserIdentifier" : @"userId",
@"UserName" : @"userName",
@"Name" : @"name",
@"Email" : @"emailAddress",
@"Bio" : @"bio",
@"Website" : @"webSite",
@"Avatar" : @"avatar"
}];
RKObjectMapping *mapping = ];
[mapping addAttributeMappingsFromDictionary:@{
@"UserIdentifier" : @"userId",
@"UserName" : @"userName",
@"Name" : @"name",
@"Bio" : @"bio",
@"Website" : @"webSite",
@"Avatar" : @"avatar",
@"Posts" : @"posts",
@"Followers" : @"followers",
@"Following" : @"following"
}];
</code></pre>
<p>但是为什么 RestKit 会选择其中一个呢?如何成功调试? RKRoute 和 RKObjectMapping 有没有关系。 PrivateUserProfile 的映射被用于序列化步骤,那么为什么不将它用于对应的反序列化,以及如何使用它?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您需要两个响应描述符 - 一个带有路径 <code>/api/v1/privateprofile/</code> 用于类 <code>PrivateUserProfile</code>,另一个带有路径 <code>/api/v1/<code>UserProfileAndStatistics</code> 类的 profile/:userId</code>:</p>
<pre><code>[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)]
]];
</code></pre></p>
<p style="font-size: 20px;">关于ios - RestKit:POST 反序列化 - 期望 A 类的对象映射,提供者为 B 返回一个,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/14205205/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/14205205/
</a>
</p>
页:
[1]