您好我有以下格式的JSON:
{
"entityList": [
{
"type": "Author",
"code" : "XYZ"
"Books": [
{
"id": 10,
"name": "book1"
"publisher": {
"code": "ABC"
}
}
],
}
]
}
这些是诱惑
@interface Author :NSManagedObject
@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) NSString * code;
@property (nonatomic, retain) NSArray* Books;
@end
@interface Book :NSManagedObject
@property (nonatomic, retain) NSNumber * id;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Publisher* publisher;
@end
@interface Publisher :NSManagedObject
@property (nonatomic, retain) NSString * code;
@end
这是映射
RKEntityMapping* Authormapping = [RKEntityMapping mappingForEntityForName:@"Author" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];
// setup the identification Attribute
Authormapping.identificationAttributes = @[@"code"];
// set up the attribute mapping
[Authormapping addAttributeMappingsFromDictionary:@{@"code":@"code",@"Books":@"Books"
}];
RKEntityMapping* Bookmapping = [RKEntityMapping mappingForEntityForName:@"Book" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];
Bookmapping.identificationAttributes = @[@"id"];
// set up the attribute mapping
[Bookmapping addAttributeMappingsFromDictionary:@{@"id":@"id"
[l_mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"publisher"
toKeyPath:@"publisher" withMapping:[Publisher getMappingForRelationship]]];
这只是拥有一系列词典的书籍,我无法弄清楚如何让NsArray包含Book entites的对象。非常感谢您的帮助。
@wain:当我在核心数据中建立关系时,该类变为
@interface Author :NSManagedObject
@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) NSString * code;
@property (nonatomic, retain) Book* Books;
@end
然后我使用关系船映射来映射书籍。但那时它只会是一本书。我无法看到我将如何将Books设置为NSSet * / NSArray *,并将其声明为Core数据中作者的关系。我不知道如何设置映射。如果不是太麻烦,请你给一个代码示例。非常感谢你。
答案 0 :(得分:0)
很抱歉没有立即回复。 Wain的解决方案是正确的。我错过链接的原因是因为一个svn问题,在我的旧文件中仍然出现,所以所有核心数据生成的方法和NSSet都丢失了。对于那些不熟悉RestKit /核心数据并遇到此问题的人。如果您有要映射的核心数据实体的对象的嵌套数组,则生成一个"一对多"关系。并请确保您重新生成课程。最好的是删除旧的。