ios - 使用 RestKit 映射空响应的对象
<p><p>感谢 <a href="https://stackoverflow.com/questions/9743155/restkit-mapping-with-an-array-of-complex-objects" rel="noreferrer noopener nofollow">help here</a>在 RestKit 邮件列表上,我已经能够解析我的 JSON,但现在我遇到了一个新问题,解析一个空响应。下面是当我的查询有结果时 JSON 的样子:</p>
<pre><code> {"blobsList":
{"blobs":
[
{"createdOn":"2012-03-16T15:13:12.551Z","description":"Fake description ","hint":"And a useless hint","id":400,"name":"Fake CA one","publicId":"FF6","type":0},
{"createdOn":"2012-03-16T17:33:48.514Z","description":"No hint on this one, but it does have a description.","hint":"Hint","id":402,"name":"Second fake one in CA","publicId":"FF8","type":0}
]}}
</code></pre>
<p>所以我将它添加到我的映射中:</p>
<pre><code> RKObjectMapping* blobsListMapping = ];
;
[.mappingProvider setMapping:blobMapping forKeyPath:@"blobsList.blobs"];
[.mappingProvider setMapping:blobsListMapping forKeyPath:@"blobsList"];
</code></pre>
<p>并且是我的类(class):</p>
<pre><code> @interface GetResponseInRegionResponse : NSObject
{
NSString* name;
NSString* blobId;
NSString* description;
NSString* hint;
}
@interface GetResponseInRegionResponseList : NSObject
{
NSArray *blobsList;
}
</code></pre>
<p>但现在问题是我的服务器也可以为 JSON 返回这个:</p>
<pre><code> {"blobsList":""}
</code></pre>
<p>是的,如果查询没有结果,我会返回。它使我的应用程序崩溃</p>
<pre><code> restkit.object_mapping:RKObjectMapper.m:255 Performing object mapping sourceObject: {
blobsList = "";
}
and targetObject: (null)
2012-03-22 11:56:16.233 Ferret T restkit.object_mapping:RKObjectMapper.m:269 Examining keyPath 'blobs' for mappable content...
2012-03-22 11:56:16.233 Ferret D restkit.object_mapping:RKObjectMapper.m:279 Found unmappable value at keyPath: blobs
2012-03-22 11:56:16.233 Ferret T restkit.object_mapping:RKObjectMapper.m:269 Examining keyPath 'blobsList.blobs' for mappable content...
2012-03-22 11:56:16.239 Ferret *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0xdb1d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key blobs.'
</code></pre>
<p>所以我试图想出一种方法让 ResKit 映射这个空响应。我已经尝试了所有这些:</p>
<pre><code>[.mappingProvider setMapping:blobListMapping forKeyPath:@"blobsList"];
[.mappingProvider setMapping:NULL forKeyPath:@"blobsList"];
;
;
</code></pre>
<p>但它们都崩溃了。我正在尝试对源代码进行正面和反面,“此类不符合 keyblob 的 key 值编码”令人费解,因为那里没有任何 blob,只有 blobsList。感谢您的帮助,谢谢!</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我在我的项目中遇到了类似的情况。我正在发布到 Web 服务,并且该服务将我发布的对象返回给我,该对象包含在一个数组中。这导致 RestKit 在解析时崩溃,因为它期望只返回一个对象而不是集合。为了解决这个问题,我实现了 objectLoader:willMapData: 委托(delegate)方法并将响应对象拉出数组并通过 RestKit 解析发送该对象,一切都很好。</p>
<p>也许你可以做类似的事情。检查您是否在 blobslist 中取回了一个字符串,并将其转换为一个空数组,然后再通过 RestKit 解析发送它。</p>
<pre><code>- (void)objectLoader:(RKObjectLoader *)objectLoader willMapData:(id *)mappableData
{
// Horrible hack to convert posted object that gets returned as an array into a single object
// that can be mapped.
//
if (])
{
if ([*mappableData isKindOfClass:])
{
*mappableData = [*mappableData objectAtIndex:0];
}
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用 RestKit 映射空响应的对象,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/9826310/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/9826310/
</a>
</p>
页:
[1]