我正在收到像这样的
JSON响应
{
"id" : 12345
"course_name" : "history",
"teacher" : "joy",
"region" : {
"code" : "Al",
"name" : "Alabama"
}
}
我在coredata中有一个课程实体,在代码中有一个相应的模型作为“MKCourse”,这个实体是这样的
MKCourse
- id
- courseName
- teacher
- regionCode
- regionName
我在MKCourse中设置嵌套字典的值,如下所示 –
mapping = [RKManagedObjectMapping mappingForClass:[self class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]];
mapping.setDefaultValueForMissingAttributes = YES;
mapping.setNilForMissingRelationships = YES;
[mapping mapKeyPathsToAttributes:
@"id", [self modelIdAttribute],
@"course_name", @"courseName",
@"teacher", @"teacher",
@"region.code", @"regionCode",
@"region.name", @"regionName",
nil];
但它始终设置为regionCode和regionName为零.我不知道出了什么问题.是否有可能获得这样的价值观.
最佳答案 对于RestKit 2.添加以下代码:
[mapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"region"];
并尝试addAttributeMappingsFromDictionary方法
[mapping addAttributeMappingsFromDictionary:@{
@"id", [self modelIdAttribute],
@"course_name", @"courseName",
@"teacher", @"teacher",
@"region.code", @"regionCode",
@"region.name", @"regionName"
}];
不确定RestKit 1.0.也许你可以尝试将它们分开:
[mapping mapKeyPath:@"id" toAttribute:[self modelIdAttribute]];
[mapping mapKeyPath:@"course_name" toAttribute:@"courseName"];
[mapping mapKeyPath:@"teacher" toAttribute:@"teacher"];
[mapping mapKeyPath:@"region.code" toAttribute:@"regionCode"];
[mapping mapKeyPath:@"region.name" toAttribute:@"regionName"];