我正在尝试使用 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”,但它没有用。