ios 核心数据:多对多关系的 NSPredicate。(“此处不允许多对多键”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4217849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:07:05  来源:igfitidea点击:

Core Data: NSPredicate for many-to-many relationship. ("to-many key not allowed here")

core-dataiosnspredicate

提问by Oh Danny Boy

I have two entities named "Category" and "Article" which have a many to many relationship. I want to form a predicate which searches for all articles where category.name is equal to some value. I have the following:

我有两个名为“类别”和“文章”的实体,它们具有多对多的关系。我想形成一个谓词,它搜索所有 category.name 等于某个值的文章。我有以下几点:

 NSEntityDescription  *entityArticle   = [NSEntityDescription entityForName:@"Article" inManagedObjectContext:managedObjectContext]; 
 NSSortDescriptor  *sortDescriptor   = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
 NSArray     *sortDescriptors  = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
 NSPredicate    *predicate    = [NSPredicate predicateWithFormat:@"categories.name == [cd] %@", category.name]; 

 [request setSortDescriptors:sortDescriptors];
 [request setEntity:entityArticle];
 [request setPredicate:predicate];

 NSMutableArray *results = [[managedObjectContext executeFetchRequest:request error:nil] mutableCopy];

 if ([results count] > 0)
  NSLog(@"Results found."); 
 else 
  NSLog(@"NO results found."); 

 [request release];
 [sortDescriptor release];
 [sortDescriptors release];

The error I receive is *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'

我收到的错误是 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'

Are there any options to retrieve the desired data?

是否有任何选项可以检索所需的数据?

回答by Dave DeLong

You're trying to compare a collection (categories.name) to a scalar value (category.name). You need to either use a collection comparator (CONTAINS), or use a predicate modifier (ANY/ALL/SOME, etc).

您正在尝试将集合 ( categories.name) 与标量值 ( category.name) 进行比较。你必须要么使用集合比较(CONTAINS),或使用谓词修饰符(ANY/ ALL/SOME等)。

Try using:

尝试使用:

[NSPredicate predicateWithFormat:@"ANY categories.name =[cd] %@", category.name];

Or:

或者:

[NSPredicate predicateWithFormat:@"categories.name CONTAINS[cd] %@", category.name];

回答by caleb81389

SWIFT SYNTAX

快速语法

In case anyone happens upon this writing in swift as I did...

万一有人像我一样迅速地看到这篇文章......

let predicate = NSPredicate(format: "ANY categories.name = %@", category.name!)
fetchRequest.predicate = predicate

worked for me.

为我工作。

回答by Oleh H

For those who faced the same problem. You can use INoperator instead of = / ==to look for specific objects in any Collection. Look herefor reference. No difference Swift or Objective-C

对于那些面临同样问题的人。您可以使用IN运算符而不是= / ==来查找任何集合中的特定对象。看这里以供参考。没有区别 Swift 或 Objective-C