【问题标题】:RestKit 0.20 nested Object mapping (path to object tree different)RestKit 0.20 嵌套对象映射(对象树的路径不同)
【发布时间】:2013-05-10 16:13:46
【问题描述】:

我在映射嵌套对象值时遇到问题。

我得到了两个具有以下属性的对象: 一)

class Input
@property NSString value;
@property NSString title;

b)

class Profile
@property Input myAwesomeInput;

..所以 Profile 包含一个 Input 对象。当我用 RestKit (0.20) 映射对象时,我得到了一些东西。像这样:

{ myAwesomeInput_test:{"value":"xyz","title":"a title"}}

我想要实现的是:

{myAwesomeInput_test:"xyz"}

所以我不想映射“输入”,而只是映射 Input.value。这甚至可能吗?

目前我的代码如下所示:

RKObjectMapping* inputMapping = [RKObjectMapping requestMapping];
[inputMapping addAttributeMappingsFromArray:@[@"value"]];

RKRequestDescriptor *reqDescInput = [RKRequestDescriptor requestDescriptorWithMapping:inputMapping objectClass:[Input class] rootKeyPath:nil];

RKObjectMapping* searchProfile = [RKObjectMapping requestMapping];
RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];

[searchProfile addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"myAwesomeInput" toKeyPath:@"myAwesomeInput_test" withMapping:inputMapping]];

编辑:(已解决)

好的,我解决了。希望这是人们应该这样做的方式。您可以直接从字典中寻址。

RKObjectMapping* searchProfile =  [RKObjectMapping requestMapping];
[aeSearchProfile addAttributeMappingsFromDictionary:@{
        @"myAwesomeInput.value": @"myAwesomeInput_test"
}];

RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];

【问题讨论】:

    标签: ios objective-c json restkit restkit-0.20


    【解决方案1】:

    使用键路径而不是多个映射。 试试这个:

    RKObjectMapping* searchProfile = [RKObjectMapping requestMapping];
    [searchProfile addAttributeMappingsFromDictionary:@{ @"myAwesomeInput.value" : @"myAwesomeInput_test" }];
    
    RKRequestDescriptor *reqDescSearchProfile = [RKRequestDescriptor requestDescriptorWithMapping:searchProfile objectClass:[SearchProfile class] rootKeyPath:nil];
    

    【讨论】:

    • 谢谢,我想我们同时找到/发布了答案(看看发布的时间:D 无论如何,对于其他有相同调查的人来说,这将是一个又好又快的方法。再次感谢
    猜你喜欢
    • 2013-02-02
    • 1970-01-01
    • 1970-01-01
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多