给定以下JSON:

{
   "someKey":"someValue",
   "otherKey":"otherValue",
   "features":[
      "feature1",
      "feature2",
      "feature3"
   ]
}

我将此JSON用NSManagedObjectRKMapperOperation映射到RKEntityMapping中(在此示例中,我将有2个实体映射:一个用于顶级对象,另一个用于我的Feature类)。

顶层对象映射是微不足道的:两个属性映射以及一个与功能的关系的关系一个(特征)。

我的问题是,如何将功能JSON数组映射到功能对象数组? Feature类只有一个属性name,我要在其中存储“feature1”,“feature2”等,以及对父对象的引用(最顶层的一个)。像这样:
@interface Feature : NSManagedObject

//In the implementation file both properties are declared with @dynamic.
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) MyTopLevelObject *myTopLevelObject;

@end

任何想法?

最佳答案

您需要使用nil键路径:

RKEntityMapping *featureMapping = [RKEntityMapping mappingForEntityForName:...];
[featureMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"name"]];
featureMapping.identificationAttributes = @[ @"name" ];

然后,在顶级对象映射上,定义关系:
[topMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"features" toKeyPath:@"features" withMapping:featureMapping]];

在您的功能(在模型中)中,应将myTopLevelObject定义为与顶级对象的双向关系。

07-28 01:52
查看更多