【问题标题】:Restkit 0.20 nested array of objects mapping issueRestkit 0.20 嵌套对象数组映射问题
【发布时间】:2013-03-06 00:52:48
【问题描述】:

我在使用 RestKit 0.20 映射以下 JSON 负载时遇到问题

我正在尝试将产品及其附件映射为包含以下对象的数组。我没有使用 RKEntityMapping,只是使用 RKObjectMappings,因为这个应用程序没有使用 Core Data。我已经在 RestKit wiki 上查阅了有关对象映射的文档,并查看了源代码中的测试用例以进行类似的设置,但没有运气。

{
    "Result": {
        "Product": {
            "Name": "Evil Stuff",
            "Description": "Its EVIL!",
            "Attachments": [
                {
                    "Id": "c4277b8f-5930-4fee-a166-b5f311d3a353",
                    "Name": "commands_to_recreate_db.txt",
                    "Type": "Contraindications"
                },
                {
                    "Id": "be4d2e2e-cb3e-48d2-9c7a-fbfddea3a1be",
                    "Name": "json.txt",
                    "Type": "Medication Guide"
                }
            ]
        },
        "Company": {
            "Name": "Omni Consumer Products",
            "AddressLine1": "100 Evil Dr.",
            "AddressLine2": null,
            "AddressLine3": null,
            "City": "MARS",
            "State": null,
            "Zip": "10000",
            "Country": null,
            "PhoneNumber1": "555-555-5555",
            "PhoneNumber2": null,
            "PhoneNumber3": null,
            "FaxNumber": null,
            "WebSite": null,
            "Email": null
        },
        "Representatives": []
    },
    "Messages": [],
    "Success": true
}



@interface clinProduct : NSObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *description;
@property (nonatomic, retain) NSArray *attachments;


@interface clinAttachment : NSObject

@property (strong, nonatomic) NSString *attachmentID;
@property (strong, nonatomic) NSString *attachmentName;
@property (strong, nonatomic) NSString *type;

这是我的对象映射和我的 RKResponseDescriptor

  RKObjectMapping *attachmentMapping = [RKObjectMapping mappingForClass:[clinAttachment class]];
  [attachmentMapping addAttributeMappingsFromDictionary:@{@"Id"   : @"attachmentID",
                                                         @"Name" : @"attachmentName",
                                                         @"Type" : @"type"}];
  RKObjectMapping *productMapping = [RKObjectMapping mappingForClass:[clinProduct class]];
  [productMapping addAttributeMappingsFromDictionary:@{@"Description" : @"description",
                                                              @"Name" : @"name"}];
  RKRelationshipMapping *relationshipMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"Attachments"
                                                                                           toKeyPath:@"attachments"
                                                                                         withMapping:attachmentMapping];
  [productMapping addPropertyMapping:relationshipMapping];
  RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:productMapping
                                                                pathPattern:nil
                                                                    keyPath:@"Result.Product"
                                                                statusCodes:[NSIndexSet indexSetWithIndex:200]];

我得到了一个有效的响应,但奇怪的是它只映射了产品的描述属性,甚至没有它的名称!

2013-03-05 18:39:49.775 iOS[38965:c07] Loaded results: <RKMappingResult: 0x96a8950, results={
    "Result.Product" = "Its EVIL!";

当我添加第二个响应描述符时,深入到 Result.Product.Attachments,我可以获得一个附件数组,仅使用附件响应描述符没有问题。任何帮助将不胜感激,自周日下午以来,我一直在努力解决这个问题。为代码墙道歉,但如果没有所有部分,很难完全描述 RestKit 设置。谢谢。

【问题讨论】:

  • 我已经解决了这个问题。首先,我粘贴的 RKMappingResult 不正确。它正在映射一个项目,但它只显示它的一个属性。为了使其工作,我删除了产品类中的 RKRelationshipMapping 和 NSAray,并为附件添加了第二个 RKResponseDescriptor。一切都在 RKMappingResult 中正确显示,不幸的是该数组只包含异构元素。这是一个 hack,但它最终解决了我的问题。
  • 你能把它作为答案发布吗?然后将其标记为答案?

标签: ios objective-c rest restkit


【解决方案1】:

为了方便用户并使答案更容易被用户看到:

@Jorge Chao 对自己问题的评论(答案):

这个问题的答案:

我已经解决了这个问题。首先,我粘贴的RKMappingResult 不正确。它正在映射一个项目,但它只显示它的一个属性。为了使它工作,我删除了产品类中的RKRelationshipMappingNSAray,并为附件添加了第二个RKResponseDescriptor

RKMappingResult 中的所有内容都正确显示,不幸的是,该数组仅包含异构元素。这是一个 hack,但它终于解决了我的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多