在restkit映射中设置静态值

时间:2013-09-16 13:11:03

标签: ios objective-c mapping restkit

在映射实体时我怎么设置静态值?

我有这样的JSON响应:

"friends": [
    {
        "id": 123,
        "name": "Friend",
    },
]
"featured": [
    {
        "id": 456,
        "name": "Some Featured user",
    },
]

我的映射和描述符如下所示:

RKMapping *friendsMapping = [ProjectMappingProvider userMapping];
RKMapping *featuredMapping = [ProjectMappingProvider featuredUserMapping];

RKResponseDescriptor *friendsResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:friendsMapping
        method:RKRequestMethodGET
        pathPattern:@"/api/users"
        keyPath:@"friends"
        statusCodes:statusCodeSet];

RKResponseDescriptor *featuredResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:friendsMapping
        method:RKRequestMethodGET
        pathPattern:@"/api/users"
        keyPath:@"featured"
        statusCodes:statusCodeSet];

RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request
        responseDescriptors:@[
        friendsResponseDescriptor,
        featuredResponseDescriptor]];

... some code emited for readabilty ...

现在mu friendsResponseDescriptor和featuredResponseDescriptors看起来几乎相同,但我想相应地设置额外的CoreData参数。通过friendsDescriptor映射的对象应该具有section = 0,并且通过特征描述符映射的对象应该具有section = 10。

那么,我可以这样做吗?

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"User"
                                               inManagedObjectStore:[[DataModel sharedDataModel] objectStore]];
[mapping addAttributeMappingsFromDictionary:@{
                                              @"id": @"userId",
                                              @"name": @"name"                                                  }];
mapping.identificationAttributes = @[ @"userId" ];

// How can I do somethning like this?
[mapping setValue:@0 forKey:@"section"];

特色映射:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"User"
                                               inManagedObjectStore:[[DataModel sharedDataModel] objectStore]];
[mapping addAttributeMappingsFromDictionary:@{
                                              @"id": @"userId",
                                              @"name": @"name"                                                  }];
mapping.identificationAttributes = @[ @"userId" ];

// How can I do somethning like this?
[mapping setValue:@10 forKey:@"section"];

请注意,我没有任何其他指标,无论用户是朋友还是用户JSON本身。我可以区分用户类型(朋友,特色)的唯一方法是在用户设置的JSON响应中的哪个列表。 我稍后在表视图控制器中使用section属性来创建节。

1 个答案:

答案 0 :(得分:0)

如果您使用的是不同的实体,请在它们上设置默认值。如果您没有使用不同的实体,请考虑更改以便您(它们可以是共同父项的子实体)。

您无法将数据插入映射中。映射仅描述了RestKit应处理的内容。要编辑值,您需要自己参与映射过程并实现一些委托方法来注入其他数据。