2

我正在尝试使用 Restkit 映射对象。我映射正常的 JSON 响应没有问题。在这种情况下,我有一个问题,因为我想用这个配置映射一个响应:

{
    "id": 161,
    "text": "usertest",
    "test": {
        "id": "1",
        "beginDate": "17/04/2013",
        "expectedDuration": "45 minutes",
        "finishDate": "17/04/2013",
        "groups": [
            {
                "id": 71,
                "text": "some text",
                "number":"number"
            },
            {
                "id": 81,
                "text": "some anothertext
            }
        ]
        ...
}

如您所见,我有一个具有三个属性(id、文本和测试列表)的主对象。每个测试都有一些属性(id、beginDate、...、)。每个测试还有一个组列表。这是我的问题。当我尝试映射它时,restkit 无法映射“组”列表。

我用这段代码映射这个:

//### TEST
RKEntityMapping *testCompleteMapping = [RKEntityMapping mappingForEntityForName:@"UserTest" inManagedObjectStore:managedObjectStore];
[testCompleteMapping addAttributeMappingsFromDictionary:@{
 @"id":             @"usertestID",
 }];

RKEntityMapping *usertest_TestMapping = [RKEntityMapping mappingForEntityForName:@"Test" inManagedObjectStore:managedObjectStore];
[usertest_TestMapping addAttributeMappingsFromDictionary:@{
 @"id":             @"testID",
 @"beginDate":            @"beginDate",
 @"expectedDuration":@"expectedDuration",
 @"finishDate":         @"finishDate",
 }];

RKRelationshipMapping* usertest_Test_Relationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"test" toKeyPath:@"test" withMapping:usertest_TestMapping];

//### GROUPS

RKEntityMapping *groupTestMapping = [RKEntityMapping mappingForEntityForName:@"QuestionGroup" inManagedObjectStore:managedObjectStore];
[groupTestMapping addAttributeMappingsFromDictionary:@{
 @"id":             @"groupID",
 @"number":            @"number",
 @"text":@"text",
 }];

RKRelationshipMapping* group_Test_Relationship = [RKRelationshipMapping relationshipMappingFromKeyPath:@"groups" toKeyPath:@"groups" withMapping:usertest_TestMapping];


//### ADD PROPERTY MAPPINGS 

RKResponseDescriptor *CompleteTestResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:testCompleteMapping pathPattern:@"/api/module/demo2/lesson/Phasellus/test/71" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


[testCompleteMapping addPropertyMappingsFromArray:[NSArray arrayWithObjects:usertest_Test_Relationship, nil]];
[usertest_TestMapping addPropertyMapping:group_Test_Relationship];

[objectManager addResponseDescriptor:CompleteTestResponseDescriptor];

我也尝试使用“test.groups”而不是“groups”,但它没有用。

4

1 回答 1

2

试试下面的代码。您的主要问题似乎是一个错字(在 的定义中group_Test_Relationship),您还缺少一些标识属性(不是严格要求),但通常代码在正确的路径上。

您没有显示您的定义,testCompleteMapping因此您需要添加它并检查拼写错误。

//### TEST

RKEntityMapping *usertest_TestMapping = [RKEntityMapping mappingForEntityForName:@"Test" inManagedObjectStore:managedObjectStore];
[usertest_TestMapping addAttributeMappingsFromDictionary:@{
 @"id": @"testID",
 @"beginDate": @"beginDate",
 @"expectedDuration": @"expectedDuration",
 @"finishDate": @"finishDate",
 }];
 usertest_TestMapping.identificationAttributes = @[ @"testID" ];

//### GROUPS

RKEntityMapping *groupTestMapping = [RKEntityMapping mappingForEntityForName:@"QuestionGroup" inManagedObjectStore:managedObjectStore];
[groupTestMapping addAttributeMappingsFromDictionary:@{
 @"id": @"groupID",
 @"text": @"text",
 }];
 groupTestMapping.identificationAttributes = @[ @"groupID" ];

[usertest_TestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"groups" toKeyPath:@"groups" withMapping:groupTestMapping]];


// complete your 'testCompleteMapping' here, adding the 'test' relationship property mapping to 'usertest_TestMapping' if required


//### ADD PROPERTY MAPPINGS 

RKResponseDescriptor *completeTestResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:testCompleteMapping pathPattern:@"/api/module/demo2/lesson/Phasellus/test/71" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor:completeTestResponseDescriptor];
于 2013-05-10T07:56:22.127 回答