核心数据:用于多对多关系的 NSPredicate。 (“此处不允许使用多对多密钥”)
我有两个名为“类别”和“文章”的实体,它们具有多对多关系。我想形成一个谓词来搜索所有类别.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尝试将集合 (
categories.name
) 与标量值 (category.name
) 进行比较。您需要使用集合比较器 (CONTAINS
),或使用谓词修饰符 (ANY
/ALL
/SOME
等)。尝试使用:
或:
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:
Or:
SWIFT 语法
万一有人像我一样快速地看到这篇文章......
为我工作。
SWIFT SYNTAX
In case anyone happens upon this writing in swift as I did...
worked for me.
对于那些面临同样问题的人。您可以使用 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