ios - RestKit 0.20嵌套对象映射(对象树的路径不同)

标签 ios objective-c json restkit restkit-0.20

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

我有两个具有以下属性的对象:
一种)

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

b)
class Profile
@property Input myAwesomeInput;

..so概要文件包含一个Input对象。当我用RestKit(0.20)映射对象时,我得到了……。像这样:
{ myAwesomeInput_test:{"value":"xyz","title":"a title"}}

我想要实现的是:
{myAwesomeInput_test:"xyz"}

因此,我不想映射“Input”,而只映射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];

最佳答案

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

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

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

关于ios - RestKit 0.20嵌套对象映射(对象树的路径不同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16486516/

相关文章:

ios - 如何在父 View 上添加 subview Controller 的 View

ios - 如何加快在 Swift 中从 GPS 获取坐标位置?

ios - 如何在 iOS SDK 中从 Facebook 新闻提要中获取高分辨率照片

iphone - 函数应该返回 float,但它变得一团糟!

ios - 如何从委托(delegate)方法 cellForRowAtIndexPath : 内部调用的方法编辑完成 block 中的 UITableViewCell

javascript - 根据 html 选择值中的数据为 FullCalendar 提供 json feed url

使用 Swift 的 SpriteKit 中的 iOS 应用程序在 6 分钟后滞后,知道为什么吗?

ios - 顶部工具栏和导航栏常用模糊效果

java - 反序列化多态 JSON 字符串

java - 使用 Jackson 进行 JSON 反序列化