【问题标题】:RestKit nested mapping to specific array itemRestKit 嵌套映射到特定数组项
【发布时间】:2012-05-28 10:52:34
【问题描述】:

是否可以通过 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;
}];

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

【问题讨论】:

    标签: ios core-data mapping restkit


    【解决方案1】:

    认为您必须为每个级别进行映射,因此实体对象映射包含一个字段区域对象。 field 区域对象映射包含一个 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];
    

    【讨论】:

    • 谢谢。我在映射中研究了这个映射,但认为简化服务器响应更明智(也更容易)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    相关资源
    最近更新 更多