我遇到了与一对多关系相关的CoreData问题。NSManagedObjects问题之间的实体关系
实体1 - Authors
与实体2具有一对多的关系 - Books
。我认为Books
与Authors
有一对一的关系。
由于每个作家的多本书籍的作者对象我有
@property (nonatomic, retain) NSSet *book;
书中对象相应的属性是:
@property (nonatomic, retain) Authors *author;
当来自服务器的应用程序下载的书籍通过API,在保存图书后,我正在尝试保存作者并使用以下代码将作者与作者相关联:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Books" inManagedObjectContext:self.managedObjectContext];
NSManagedObject *record = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
record setValue:bookname forKey:@"bookname"];
if ([self.managedObjectContext save:&error]) {
Authors *newAuthor = [NSEntityDescription insertNewObjectForEntityForName:@"Authors" inManagedObjectContext:self.managedObjectContext];
newAuthor.aid = authorid;
newAuthor.book = record;
}
此代码为我工作了一对一的关系,但在这种情况下,抛出下面的异常错误:
'NSInvalidArgumentException', reason: 'Unacceptable type of value for to-many relationship: property = "book"; desired type = NSSet; given type = Books;
任何人都可以建议如何解决这一问题?
更新:
我也试过围绕切换它来阅读:
record.author = newAuthor;
但是这给了错误
"property 'author' not found on object of type NSManagedObject"
虽然有这样的Books
对象定义的属性(如如上所示)。
对不起,是密集的,但我一直在打我的头在这一段时间...书*记录= ???? – zztop
如果核心数据创建的方法是这个引擎盖下还是我应该做些什么? – zztop
我试图解决这个问题。让我知道我们是如何做的。 :) –