4

我收到这样的 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 设置为 nil。我不知道出了什么问题。是否有可能获得这样的值。

4

1 回答 1

2

对于 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"];
于 2013-05-06T09:55:14.007 回答