RKMappingResult用于多个映射

时间:2014-06-22 05:37:42

标签: objective-c design-patterns path mapping restkit

我正在发送有关用户的请求。有几种可能性。我正在考虑发送这样的JSON:

"type1": {
    "id": 34,
    "email": "email@test.com",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}
"type2": {
    "id": 34,
    "email": "email@test.com",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}
"type3": {
    "id": 34,
    "email": "email@test.com",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}

然后我在考虑多个RKResponseDescriptor,但我不知道如何解释RKMappingResult。我有办法将对象引用到keyPath吗?在这里,我使用相同RKEntityMapping的3倍但使用不同的keyPath。

最后我甚至不知道是否可能。

修改

也许这种JSON更好更容易理解/映射。

"type" : "type1", 
"user": {
    "id": 34,
    "email": "email@test.com",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

没有足够的信息来推荐JSON结构。任意键不是很好,但它很常见。通常是一个容器' JSON中的对象更好。

也就是说,RestKit通常可以处理这两种情况。查看RKObjectMapping上的addAttributeMappingFromKeyOfRepresentationToAttribute:addAttributeMappingToKeyOfRepresentationFromAttribute:,了解如何处理此类JSON的示例。

答案 1 :(得分:0)

如果您的问题与如何使用Restkit映射映射响应JSON有关,那么这是设计JSON的更好方法。

{"userProfile":[
{
    "id": 34,
    "email": "email@test.com",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
},
{
    "id": 34,
    "email": "email@test.com",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
},
 {
    "id": 34,
    "email": "email@test.com",
    "firstname": "Juan",
    "lastname": "Ito",
    "gender": 2,
    "created_at": "2012-09-21T18:53:02Z",
    "updated_at": "2012-12-27T16:22:47Z"
}
]}

模态课程:

userProfile.h

@property(nonatomic,strong) NSMutableArray *userInfo;

<强> userInfo.h

@property(assign) int id;
@property(nonatomic,strong) NSString *email;

.......

可以使用 AttributeMapping RelationshipMapping

进行映射

答案 2 :(得分:0)

我终于映射了type这样:

RKObjectMapping *typeMapping = [RKObjectMapping requestMapping];
[typeMapping addAttributeMappingsFromDictionary:@{@"type":@"type"}];

使用此描述符:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:typeMapping
                                                                                         method:RKRequestMethodAny
                                                                                    pathPattern:nil
                                                                                        keyPath:nil
                                                                                    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

基本上要抓住这本字典,我基本上就这样做了:

NSString *type = [((NSDictionary *)[[mappingResult array] objectAtIndex:0]) objectForKey:@"type"];

这解决了我的问题,因为在我根据type的值选择不同的操作之后。

我让@Wain告诉我们这种方法是否可以接受。