1

次の JSON があります。

{
    "stream": [{
        "catalog": {}
    }, {
        "catalog": {}
    }, {
        "user": {}
    }]
}

次のマッピング ルールを使用します。

[RKResponseDescriptor responseDescriptorWithMapping:catalogMapping
                                        pathPattern:nil keyPath:@"stream.catalog"
                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

[RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                        pathPattern:nil keyPath:@"stream.user"
                                        statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

複数のタイプのオブジェクトがストリームに含まれるまで、マッピングは正常に機能します。ストリームに複数のオブジェクトが含まれている場合、ユーザーとカタログのレストキットが次のアサートに失敗するとします。

NSCAssert([representation isKindOfClass:[NSDictionary class]], @"Expected a dictionary representation");

その構造をマップする方法はありますか?

4

1 に答える 1

3

API の変更をリクエストする余裕があったので、

{
    "stream": [{
        "type": "catalog",
        "other": "fields"
    }, {
        "type": "catalog"
        "other": "fields"
    }, {
        "type": "user"
        "other": "fields"
    }]
}

動的マッピングを使用するようになりました。

    RKDynamicMapping* dynamicMapping = [RKDynamicMapping new];
    [dynamicMapping setObjectMappingForRepresentationBlock:^RKObjectMapping *(id representation) {
        if ([[representation objectForKey:@"type"] isEqualToString:@"catalog"]) {
            return catalogMapping;
        } else if ([[representation objectForKey:@"type"] isEqualToString:@"user"]) {
            return userMapping;
        }
//       ...
    }];


    [manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:dynamicMapping
                                                                           pathPattern:nil keyPath:@"stream"
                                                                           statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
于 2013-05-31T09:16:29.003 に答える