核心数据:用于多对多关系的 NSPredicate。 (“此处不允许使用多对多密钥”)

发布于 2024-10-02 19:45:18 字数 1052 浏览 3 评论 0原文

我有两个名为“类别”和“文章”的实体,它们具有多对多关系。我想形成一个谓词来搜索所有类别.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];

我收到的错误是 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“此处不允许使用多键”

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

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:

 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'

Are there any options to retrieve the desired data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆一缕阳光ご 2024-10-09 19:45:18

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

尝试使用:

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

或:

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

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).

Try using:

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

Or:

[NSPredicate predicateWithFormat:@"categories.name CONTAINS[cd] %@", category.name];
紫南 2024-10-09 19:45:18

SWIFT 语法

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

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

为我工作。

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.

很快妥协 2024-10-09 19:45:18

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文