RestKit嵌套到特定数组项的映射

时间:2012-05-21 22:57:47

标签: ios core-data mapping restkit

可以通过mapKeyPath点语法强制RestKit对象映射器映射到数组中的特定嵌套对象吗?

这是我想要映射的回复

<result is_array="true">
  <item>
     <_field_data>
        <nid>
           <entity>
              <field_region>
                 <und is_array="true">
                    <item>
                       <value>1</value>
                    </item>
                 </und>
              </field_region>
....

我只对将要为field_region返回的第一个项目感兴趣。

映射此图的正确方法是什么?

我尝试了各种迭代:

RKManagedObjectMapping *record = [RKManagedObjectMapping mappingForClass:[MyRecordClass class] inManagedObjectStore:objManager.objectStore];
[record mapKeyPath:@"_field_data.nid.entity.field_region.und.0.item.value" toAttribute:@"region"];

我不断得到类似的错误:

T restkit.object_mapping:RKObjectMappingOperation.m:152 Found transformable value at keyPath '_field_data.nid.entity.field_region.und.0.item.value'. Transforming from type '__NSArrayI' to 'NSNumber'
W restkit.object_mapping:RKObjectMappingOperation.m:232 Failed transformation of value at keyPath '_field_data.nid.entity.field_region.und.0.item.value'. No strategy for transforming from '__NSArrayI' to 'NSNumber'

我通过以下方式调用映射:

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:kMyResourcePath usingBlock:^(RKObjectLoader* loader) {
    loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[MyRecordClass class]];
    loader.method = RKRequestMethodGET;
    loader.delegate = self;
    loader.targetObject = nil;
}];

有什么想法吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

认为你必须为每个级别都有映射,因此实体对象映射包含一个字段区域对象。字段区域对象映射包含und对象。 und对象映射包含项目等...

我只使用嵌套的json rest kit响应,而不是xml,但必须为每个级别创建映射。

像这样的事情(授予这个是不完整的,因为你需要每个级别的关系)

RKObjectMapping* undMapping = [RKObjectMapping mappingForClass:[UND class]];
[und mapKeyPath:@"item" toAttribute:@"item"];

RKObjectMapping* regionmapping = [RKObjectMapping mappingForClass:[Region class]]; 
[regionmapping mapKeyPath:@"und" toRelationship:@"undArrays" withMapping:undMapping];