2013-05-10 120 views
2

我试图用Restkit映射对象。我没有问题映射正常的JSON响应。在这种情况下,我有一个问题,因为我要地图这个配置的响应:ios Restkit - 用另一个列表中的列表映射对象

{ 
    "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”,但没有奏效。

回答

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]; 
+0

我忘了显示testCompleteMapping的定义并显示正确的JSON代码(有一个数字字段)。现在,一切都是正确的。我会很快尝试你的代码。 – 2013-05-10 09:43:27

+0

你说得对。我做了一个错误,创建realtionship。[usertest_TestMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@“groups”toKeyPath:@“groups”withMapping:groupTestMapping]];这条线解决了我的问题。 – 2013-05-10 14:32:11

相关问题