0
嗨,我有以下格式的JSON:休息套件實體映射一個嵌套的數組
{
"entityList": [
{
"type": "Author",
"code" : "XYZ"
"Books": [
{
"id": 10,
"name": "book1"
"publisher": {
"code": "ABC"
}
}
],
}
]
}
這些都是entites的
@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包含圖書entites的對象。非常感謝你的幫助。
@wain:當我在覈心數據進行關係類成爲
@interface Author :NSManagedObject
@property (nonatomic, retain) NSString * type;
@property (nonatomic, retain) NSString * code;
@property (nonatomic, retain) Book* Books;
@end
然後我用船的關係映射地圖冊。但它只會是一本書。我看不出我將如何將書籍設置爲NSSet */NSArray *,並將其聲明爲與核心數據中作者的關係。我不知道如何設置映射。如果不是太麻煩,請給我一個代碼示例。非常感謝你。
爲什麼'NSArray'?在覈心數據中你應該使用關係,所以它應該是'NSSet'。然後使用'RKRelationshipMapping'。 – Wain
因此,這應該是@property(nonatomic,retain)NSSet *書籍和映射將是[Authormapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@「Books」toKeyPath:@「Books」withMapping:[Book getMapping]];我對嗎? – user3678218
當我這樣做,我得到NSManagedObjects的實體'作者'不支持-mutableSetValueForKey:屬性'書籍'。 BTW書是作者的一個屬性。 – user3678218