Restkit 0.20嵌套数组对象映射问题

时间:2013-03-06 00:52:49

标签: ios objective-c rest restkit

我在使用RestKit 0.20

映射以下JSON有效负载时遇到问题

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

{
    "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设置完全没有所有的部分。谢谢。

1 个答案:

答案 0 :(得分:2)

为用户提供便利并使答案更加清晰:

@Jorge Chao就自己的问题发表评论(回答):

回答此问题:

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

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