2013-03-06 31 views
6

的嵌套数组。我没有使用RKEntityMapping,只是RKObjectMappings,因为这个应用程序没有使用核心数据。我已经查阅了RestKit wiki上Object Mappings的文档,并在源代码中查看了测试用例中的类似设置,但没有运气。Restkit 0.20我有一个问题,映射使用RestKit 0.20</p> <p>我试图映射的产品及其附件具有下列对象的数组下面的JSON有效载荷对象映射问题

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

+2

我已经解决了这个问题。首先,我粘贴的RKMappingResult不正确。它映射一个项目,但它只显示其中一个属性。为了使它工作,我删除了产品类中的RKRelationshipMapping和NSAray,并为附件添加了第二个RKResponseDescriptor。一切都在RKMappingResult中正确显示,数组只是不幸地包含异构元素。这是一个黑客,但它终于解决了我的问题。 – 2013-03-10 05:09:30

+4

你可以把它作为答案吗?然后将其标记为答案? – Steven 2013-10-22 18:59:51

回答

2

为了方便,使答案更显眼:

@Jorge超对自己的问题评论(回答):

这个问题的答案:

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

一切正常显示在RKMappingResult,数组只是不幸地包含异构元素。这是一个黑客攻击,但它终于解决了我的问题。

+0

你能展示它在代码中的外观吗? – 2014-01-12 16:15:23