RestKit动态嵌套属性

时间:2014-02-09 16:33:49

标签: ios objective-c restkit restkit-0.20

我正在尝试映射具有动态嵌套属性的API结果。结果(json)就是这个:

{
    "url": "https://api.github.com/gists/8901308",
    "id": "8901308",
    "html_url": "https://gist.github.com/8901308",
    "files": {
        "node-and-npm-in-30-seconds.sh": {
            "filename": "node-and-npm-in-30-seconds.sh",
            "type": "application/sh",
            "language": "Shell",
            "raw_url": "https://gist.github.com/braincrash/8901308/raw/bae861f7c4ab0c1ffd9962439e770b02f52c5dd7/node-and-npm-in-30-seconds.sh",
            "size": 352
        },
        "only-git-all-the-way.sh": {
            "filename": "only-git-all-the-way.sh",
            "type": "application/sh",
            "language": "Shell",
            "raw_url": "https://gist.github.com/braincrash/8901308/raw/eba9667b37218ffb41892411c94abd051b0e269a/only-git-all-the-way.sh",
            "size": 440
        }
    }
}

我可以获取所有属性,但文件不起作用。这是我的映射:

RKEntityMapping *fileMapping = [RKEntityMapping mappingForEntityForName:@"File" inManagedObjectStore:managedObjectStore];

fileMapping.forceCollectionMapping = YES;
[fileMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"filename"];
[fileMapping addAttributeMappingsFromDictionary:@{@"(filename).raw_url": @"rawURL",
                                                  @"(filename).size": @"size"}];

RKEntityMapping *gistMapping = [RKEntityMapping mappingForEntityForName:@"Gist" inManagedObjectStore:managedObjectStore];
[gistMapping addAttributeMappingsFromDictionary:@{
                                                  @"id":             @"gistID",
                                                  @"url":            @"jsonURL",
                                                  @"html_url":            @"htmlURL"}];
gistMapping.identificationAttributes = @[ @"gistID" ];

[gistMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"files" toKeyPath:@"files" withMapping:fileMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:gistMapping method:RKRequestMethodGET pathPattern:@"/gists/public" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

当我查看Gist对象中的File时,我只获取文件名,但不是url或size:

filename = "radiation.js";
gist = "0xb9742f0 <x-coredata://D37E442D-45BA-4A0E-B7A5-A349F75FA362/Gist/p21>";
rawURL = nil;
size = 0;

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

您的文件名包含点,因此它们会弄乱关键路径导航。

RestKit确实对此感到困惑,我认为目前没有解决方案。已经提出了一些(https://github.com/RestKit/RestKit/pull/1541/files),但我认为这仍然是一个悬而未决的问题。

问题是如何知道点是否代表遍历的关键路径。理论上可以确定,但在框架或性能方面实施并不一定是微不足道的。您可以围绕RKObjectMappingNestingAttributeKeyName的使用情况进行调试,并为该部分添加自定义解决方案。