在何处放置对象映射(在RestKIt中) [英] Where to put object mappings (in RestKIt)
问题描述
由于我不想劫持另一个线程,因此我遇到了有关映射的问题.
As I don't want to hijack another thread here comes my question about mappings.
我敢肯定,布雷克·沃特斯(Blake Waters)给出的答案很可能是正确的,因为他是一个比我聪明得多,经验更丰富的人,但是对我来说,逻辑告诉我将映射放入每个模型中:如果您更改了某些内容,在模型中,只需滚动一下即可更改映射.
I'm sure that the answer Blake Waters gave will probable be very correct as he is a much smarter and more experienced guy than I am, but to me logic tells me to put the mapping in each model: if you change something in your model, you're just a scroll away to change your mappings.
然后在我的AppDelegate中,只需在每个模型中调用initMappings(或任何您想调用的对象)即可.
In my AppDelegate I would then just call the initMappings (or whatever you want to call it) in each of my models.
推荐答案
我也喜欢将映射与模型放置在一起.我通过向每个模型添加一个类方法来做到这一点,这样我就可以在需要的任何时候/任何地方获取映射.
I'm also a fan of placing the mappings with my models. I do it by adding a class method to each model so I can get the mapping whenever/wherever I need it.
+ (RKObjectMapping *)objectMapping
{
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[self class]];
[mapping mapKeyPath:@"Id" toAttribute:@"id"];
[mapping mapKeyPath:@"Subject" toAttribute:@"subject"];
[mapping mapKeyPath:@"Message" toAttribute:@"message"];
[mapping mapKeyPath:@"PostDate" toAttribute:@"postDateStr"];
[mapping mapKeyPath:@"StatusId" toAttribute:@"statusId"];
[mapping mapKeyPath:@"StatusDate" toAttribute:@"statusDateStr"];
mapping.setNilForMissingRelationships = YES;
return mapping;
}
这篇关于在何处放置对象映射(在RestKIt中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!