Object使用RestKit映射空响应

时间:2012-03-22 16:16:01

标签: ios restkit

感谢help here并在RestKit邮件列表上,我已经能够解析我的JSON,但现在我遇到了一个新问题,解析了一个空响应。要设置阶段,以下是我的查询结果时JSON的样子:

    {"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}
            ]}}

所以我把它添加到我的映射中:

        RKObjectMapping* blobsListMapping = [RKObjectMapping mappingForClass:[GetResponseInRegionResponseList class]];
        [blobsListMapping mapKeyPath:@"blobsList" toAttribute:@"blobsList"];

        [[RKObjectManager sharedManager].mappingProvider setMapping:blobMapping forKeyPath:@"blobsList.blobs"];

        [[RKObjectManager sharedManager].mappingProvider setMapping:blobsListMapping forKeyPath:@"blobsList"];

这是我的课程:

        @interface GetResponseInRegionResponse : NSObject
        {
            NSString* name;
            NSString* blobId;
            NSString* description;
            NSString* hint;
        }


        @interface GetResponseInRegionResponseList : NSObject
        {
            NSArray  *blobsList;
        }

但现在皱纹是我的服务器也可以为JSON返回:

        {"blobsList":""} 

是的,如果查询没有结果,我会回来。它崩溃了我的应用

        restkit.object_mapping:RKObjectMapper.m:255 Performing object mapping sourceObject: {
            blobsList = "";
        }
         and targetObject: (null)

        2012-03-22 11:56:16.233 Ferret[7399:17a07] T restkit.object_mapping:RKObjectMapper.m:269 Examining keyPath 'blobs' for mappable content...
        2012-03-22 11:56:16.233 Ferret[7399:17a07] D restkit.object_mapping:RKObjectMapper.m:279 Found unmappable value at keyPath: blobs
        2012-03-22 11:56:16.233 Ferret[7399:17a07] T restkit.object_mapping:RKObjectMapper.m:269 Examining keyPath 'blobsList.blobs' for mappable content...
        2012-03-22 11:56:16.239 Ferret[7399:17a07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFConstantString 0xdb1d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key blobs.'

所以我想找到一种让ResKit映射这个空响应的方法。我已经尝试了所有这些:

[[RKObjectManager sharedManager].mappingProvider setMapping:blobListMapping forKeyPath:@"blobsList"];
[[RKObjectManager sharedManager].mappingProvider setMapping:NULL forKeyPath:@"blobsList"];
[blobListMapping mapKeyPath:@"" toAttribute:@"blobsList"];
[blobListMapping mapKeyPath:@"blobsList" toAttribute:@"blobsList"];

但他们都崩溃了。我试图对源代码进行改进,“这个类不是关键值blob的关键值编码”令人费解,因为那里没有任何blob,只是blobsList。感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:3)

我在项目中碰到了类似的东西。我发布了一个Web服务,该服务正在向我发送包含在数组中的已发布对象。这导致RestKit在解析时崩溃,因为它期望只返回单个对象而不是集合。为了解决这个问题,我实现了objectLoader:willMapData:delegate方法并将响应对象从数组中拉出来,并通过RestKit解析发送了该对象,一切顺利。

也许你可以做类似的事情。检查是否在blobslist中返回一个字符串,并在通过RestKit解析发送之前将其转换为空数组。

- (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 ([objectLoader.targetObject isKindOfClass:[TSLPostPostContract class]])
    {
        if ([*mappableData isKindOfClass:[NSArray class]])
        {
            *mappableData = [*mappableData objectAtIndex:0];
        }
    }
}