给定以下 JSON:
{
"someKey":"someValue",
"otherKey":"otherValue",
"features":[
"feature1",
"feature2",
"feature3"
]
}
我将此 JSON 映射到NSManagedObject
s with RKMapperOperation
and RKEntityMapping
(在本例中,我将有 2 个实体映射:一个用于顶级对象,另一个用于我的要素类)。
顶层对象映射很简单:两个属性映射加上一个关系一(特征)用于与特征的关系。
我的问题是,如何将功能 JSON 数组映射到功能对象数组中?要素类只有一个属性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
应定义为与顶级对象的双向关系。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)