RestKit 非 kvc 对象映射 [英] RestKit non-kvc object mapping

查看:64
本文介绍了RestKit 非 kvc 对象映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 RestKit 集成到我当前的项目中.我正在使用以下格式检索数据:

<预><代码>[{"id":"1",姓名":约翰"}{"id":"2",姓名":鲍勃"}]

按照 RestKit wiki 中的优秀教程,我看到推荐的, 完全符合 KVC 的格式为:

{"客户": [{"id":"1",姓名":约翰"}{"id":"2",姓名":鲍勃"}]}

然后我可以使用

[mappingProvider setMapping:customerMapping forKeyPath:@"customers"];

很遗憾,我目前无法控制服务器上的数据格式.

稍后在 RestKit wiki 中,有一个部分可以解决我的问题:没有 KVC 的映射".我会在加载时将 objectMapping 与对象相关联.

[objectManager loadObjectsAtResourcePath:@"/data" objectMapping:customerMapping delegate:self];

这是我的问题:Wiki 适用于 0.9.3.但是此方法在 0.10.1 中已弃用.是的,它仍然有效 - 就目前而言.

如果不推荐使用此方法,并记住我不控制数据,那么在这种非 KVC 情况下将对象映射分配给类的适当方法是什么?

解决方案

我对此并不乐观,但我相信它已被弃用以支持块...这可能有效:

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/data" usingBlock:^(RKObjectLoader* loader) {[加载器 setObjectMapping:customerMapping];}];

而且,很自然地,使用块来替换委托方法.

I am experimenting with integrating RestKit into my current project. I am retrieving data with the format:

[
    {"id":"1",
    "name":"john"}
    {"id":"2",
    "name":"bob"}
]

Following the excellent tutorial in the RestKit wiki, I see that the recommended, fully KVC-compliant format would be:

{"customers": [
    {"id":"1",
    "name":"john"}
    {"id":"2",
    "name":"bob"} ]
}

Then I could use

[mappingProvider setMapping:customerMapping forKeyPath:@"customers"];

Unfortunately, I do not at the moment have control over the data format on the server.

Later in the RestKit wiki, there is a section that solves my problem: "Mapping without KVC." I would associate the objectMapping with the objects at load-time.

[objectManager loadObjectsAtResourcePath:@"/data" objectMapping:customerMapping delegate:self];

Here's my problem: The wiki is for 0.9.3. But this method is deprecated in 0.10.1. Yes, it still works - for now.

If this method is being deprecated, and keeping in mind that I don't control the data, what is the appropriate way to assign an object mapping to a class in this sort of non-KVC situation?

解决方案

I am not positive about this but I believe it is deprecated in support of blocks... this might work:

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/data" usingBlock:^(RKObjectLoader* loader) {
    [loader setObjectMapping:customerMapping];
}];

And, naturally, use the block to replace the delegate methods.

这篇关于RestKit 非 kvc 对象映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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