关于C:在restkit中映射数组

Mapping arrays in restkit

在带有Restkit的Objective-C中。

我正在尝试映射这样的对象:

{

1
2
3
4
5
6
7
8
9
10
11
"key1":"key1",
"key2":"key2",
"key3":[{
        "otherkey1":"otherkey1",
        "otherkey2":"otherkey2"
        },
        {
        "otherkey1":"otherkey1",
        "otherkey2":"otherkey2"
        },
       ]

}

我在做这样的事情:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
RKObjectManager *objMan = [[RKObjectManager alloc] initWithHTTPClient: client];

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[@"key1",
                                                @"key2",
                                                @"key3"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[objContainer class] rootKeyPath:nil method:RKRequestMethodAny];

/**/
RKObjectMapping *requestMapping1 = [RKObjectMapping requestMapping];
[requestMapping1 addAttributeMappingsFromArray:@[@"otherkey1",
                                                @"otherkey2",
                                                ]];

RKRequestDescriptor *requestDescriptor1 = [RKRequestDescriptor     requestDescriptorWithMapping:requestMapping1 objectClass:[ContainerClient class] rootKeyPath:@"key3" method:RKRequestMethodAny];

RKObjectManager *manager = [RKObjectManager sharedManager];
[manager addRequestDescriptor:requestDescriptor];

RKObjectMapping *objMapping = [RKObjectMapping mappingForClass:[objContainer class]];
[objMapping addAttributeMappingsFromDictionary:@{
                                                 @"key1" : @"key1",
                                                 @"key2" : @"key2",
                                                 @"key3" : @"key3",
                                                 }];

/**/
RKObjectManager *manager1 = [RKObjectManager sharedManager];
[manager1 addRequestDescriptor:requestDescriptor1];


RKObjectMapping *objMapping1 = [RKObjectMapping mappingForClass:[ContainerClient class]];
[objMapping1 addAttributeMappingsFromDictionary:@{
                                                 @"otherkey1" : @"otherkey1",
                                                 @"otherkey2" : @"otherkey2",
                                                 }];
/**/

集装箱客户

@property (strong, nonatomic) NSNumber* otherkey1;

@property (strong, nonatomic) NSString *otherkey2;

类ObjContainer

@property (strong, nonatomic) NSNumber* key1;

@property (strong, nonatomic) NSNumber* key2;

@property (strong, nonatomic) NSMutableArray* key3;

拥有类似于此代码的东西,以便在发送对象时与服务器进行通信,而对象没有列表和工作。如何映射列表?


您需要设置响应描述符——这些目前完全不存在。

如果key3应该包含ContainerClient的实例,那么您也需要一个关系映射。使用addRelationshipMappingWithSourceKeyPath:mapping:执行此操作。