RestKit - 将数组的键路径映射到该数组内的对象 [英] RestKit - Map keypath of an array to the object inside of that array

查看:29
本文介绍了RestKit - 将数组的键路径映射到该数组内的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的用 RestKit 0.20 解析这个特定的响应:

I really stack up parsing this particular response with RestKit 0.20:

"addons": [
    {
        "id": 1,
        "name": "Addon one",
        "version": 2
    },
    {
        "id": 2,
        "name": "Addon two",
        "version": 3
    }
],
"forms": [
    {
        "id": 1,
        "name": "Form one",
        "version": 1
    }
]

问题是我需要把响应翻译成同一个类的对象数组,比如

The problem is that I need to translate the response to an array of object of the same class, such as

@interface MyCoreDataObject : NSManagedObject

@property (nonatomic, retain) NSString * uid;
@property (nonatomic, retain) NSNumber * version;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * type;

@end

鉴于此,对象数组的键应映射到 type 属性,并且映射结果上方的响应结果应类似于对象数组(和数据库中的表):

Given that, the key for the array of object should map to the type property and as the result for the response above the mapped result should be look like an array of objects (and table in the database):

uid     name            version     type
1       Addon one       2           addons
2       Addon two       3           addons
1       Form one        1           forms

这点也很重要,ID和Type是作为身份属性使用的,所以映射完成后我无法设置类型值,因为这样RestKit将无法找到已经在DB中的对象并创建另一个而不是更新现有的.

This is also important, that ID and Type are used as an identity attribute, so I cannot set the type value after mapping is completed, because in this case the RestKit will not be able to find the object that already in the DB and create another one instead of updating that existing one.

有没有可能解决这个问题?

Does it possible to solve that problem?

谢谢大家的回答!

PS:在我挣扎的过程中,我找到了一些方法来做到这一点,比如为特定类型(用于插件、表单等)创建一个对象,或者在映射完成后设置类型,但它看起来真的很重这个简单问题的解决方案.

P.S.: During my struggling I found some ways to do it, like creating an object for particular type (for addon, for form and so on), or set type after the mapping is done, but it seams to be really heavy solutions for that simple problem.

推荐答案

我不得不说您应该创建自己的 JSON 序列化类并注册它.您的类将正常反序列化,然后检查并改变结果以分解类型信息.

I'd have to say that you should create your own JSON serialisation class and register it. You class will deserialise as normal but then check and mutate the result to decompose the type information.

忽略数组问题的原始答案:

Original answer that ignored the array problem:

您需要使用 addAttributeMappingFromKeyOfRepresentationToAttribute: 允许您提取该键并将其用作值.

You need to use addAttributeMappingFromKeyOfRepresentationToAttribute: which allows you to extract that key and use it as a value.

这篇关于RestKit - 将数组的键路径映射到该数组内的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆