Objective c COREDATA:为一对多关系添加值

Objective c COREDATA:为一对多关系添加值,objective-c,core-data,Objective C,Core Data,我有一个coredata项目: 这里我有3个实体:a)类别,b)内容,c)标签。我已经将其添加到了categories值中,但是当用户选择了他想要内容的类别时,我不知道如何在内容实体中设置类别 非常感谢您的帮助我知道如何将类别添加到内容中: NSError *error = nil; NSManagedObjectContext *moc = [self managedObjectContext]; NSEntityDescription *contentDescrip

我有一个coredata项目:

这里我有3个实体:a)类别,b)内容,c)标签。我已经将其添加到了categories值中,但是当用户选择了他想要内容的类别时,我不知道如何在内容实体中设置类别


非常感谢您的帮助

我知道如何将类别添加到内容中:

    NSError *error = nil;
    NSManagedObjectContext *moc = [self managedObjectContext];
    NSEntityDescription *contentDescription = [ NSEntityDescription entityForName:@"Categories" inManagedObjectContext:moc];

    NSFetchRequest *categoRequest = [NSFetchRequest new];
    categoRequest.entity = contentDescription;
    NSPredicate *categoPredicate = [NSPredicate predicateWithFormat:@"category like %@", _dropMenuOulet.stringValue];
    categoRequest.predicate = categoPredicate;

    NSArray *results = [moc executeFetchRequest:categoRequest error:&error];



    Categories *catego = (Categories*) [results objectAtIndex:0];
Content *content1 = [NSEntityDescription insertNewObjectForEntityForName:@"Content" inManagedObjectContext:moc];

    content1.category = catego;
    content1.title = _titleOutlet.stringValue;
    content1.body = _bodyOutlet.stringValue;

但现在我正试图找出如何将标签添加到内容中。

如果要将多个类别分配到单个内容,则内容类别应该是多对多关系。标签也一样。此外,在CoreData中,您可以用单数命名实体,例如,类别应为Category。然后,当您建立关系时,每个类别都会有内容(很多),每个内容都会有“类别”和“标记”(很多)。您需要反向关系,这将允许您获取特定类别的所有内容。所有这些都可以在关系字段的CoreData实体属性的右侧栏中设置。@Andy,在类别的情况下,我每个内容只需要一个类别。我不确定你的意思是“你需要反向关系,这将允许你获取特定类别的所有内容。”。您能提供并举例说明吗?[yourContent setValue:yourCategory forKey:@“category]!?!@Aminegm Awad,我试过了,但没有成功如果您正确设置了关系,那么您可以使用为您动态创建的访问器方法()您的content CoreData对象将具有类似于
-(void)setCategory:(category*)的方法category;
-您只需声明它,就可以访问它