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

标签 ios objective-c rest restkit

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

我正在尝试将产品及其附件映射为具有以下对象的数组。我没有使用 RKEntityMapping,只是 RKObjectMappings,因为这个应用程序没有使用核心数据。我查阅了 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 设置。谢谢。

最佳答案

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

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

这个问题的答案:

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

RKMappingResult 中的一切都正确显示,不幸的是,该数组只包含异构元素。这是一个 hack,但它最终解决了我的问题。

关于ios - Restkit 0.20 嵌套对象数组映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15237026/

相关文章:

ios - 在 Swift 中解析动态可选键值 JSON

objective-c - NSMutableParagraphStyle 与 NSMutableAttributedString

java - 没有rest或gradle的java restful api库

java - Elastic Stack - 具有完整 JSON 请求和响应的 REST API 日志记录

ios - TitleForHeaderInSection 显示不正确

ios - 使用 ios keychain 进行身份验证 - touch id(这可能吗)

ios - UINib initWithNibName 在应用加载前崩溃

ios - 使用 objc_setAssociatedObject 时架构 armv7 的 undefined symbol

objective-c - 具有 subview 和多个 NIB 的核心数据绑定(bind)

java - pd4ml 的 Maven 依赖项