【问题标题】:Mapping object from sibling node in json in RestKit在 RestKit 中从 json 中的兄弟节点映射对象
【发布时间】:2015-11-12 08:19:45
【问题描述】:

JSON

{
   "entity": {
      "id": 1,
      "name": "name"
   },
   "settings": {
      "key": "value"
   }
}

实体模型

属性:id、name

关系:设置(一对一)

设置模型

属性:键

关系:实体(反向关系,一对一)

实体映射

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([self class]) inManagedObjectStore:[RKManagedObjectStore defaultStore]];
mapping.persistentStore = [RKManagedObjectStore defaultStore].persistentStoreCoordinator.persistentStores.firstObject;

mapping.identificationAttributes = @[@"id"];
[mapping addAttributeMappingsFromDictionary:@{
                                              @"id" : @"id",
                                              @"name" : @"name"
                                              }];

如果设置在 json 响应中的实体内,我会添加这个

[mapping addPropertyMapping:
    [RKRelationshipMapping relationshipMappingFromKeyPath:@"settings" 
      toKeyPath:@"settings" withMapping:[Settings map]]
];

但我的响应 id 与此不同,那么我应该如何正确映射?

【问题讨论】:

    标签: ios core-data restkit


    【解决方案1】:

    entitysettings 都在容器字典中,因此您可以将响应描述符更改为到目前为止不向下钻取,然后使用关键路径钻入 entity 部分并直接访问 @ 987654324@部分:

    [mapping addAttributeMappingsFromDictionary:@{
                                              @"entity.id" : @"id",
                                              @"entity.name" : @"name"
                                              }];
    
    [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"settings" toKeyPath:@"settings" withMapping:[Settings map]]];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多