NSPredicate使用NONE聚合操作 [英] NSPredicate Aggregate Operations with NONE

查看:159
本文介绍了NSPredicate使用NONE聚合操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建可以提取的谓词:所有问题不包含 answer.correct =1

How do I create a predicate that can fetch: all questions does not contain answer.correct = "1".

如果返回的数组包含0和1,以下谓词不起作用:

The following predicate doesn't work if the returned array contain "0" and "1":

[NSPredicate predicateWithFormat:@"NONE answers.correct IN %@", [NSArray arrayWithObject:@"1"]];

也尝试使用NOT(ANY ...):相同结果

Also tried with NOT (ANY ...) : same result

这是否是错误?

推荐答案

[NSPredicate predicateWithFormat:@"NONE answers.correct == %@", @"1"];

[NSPredicate predicateWithFormat:@"NOT (ANY answers.correct == %@"), @"1"];

应该可以正常工作。

更新:我的第一个答案其实是错误的。它应该工作(因为我理解NOT和ANY关键字),但它不。这似乎是我的核心数据错误。

UPDATE: My first answer is actually wrong. It should work (as I understand the NOT and ANY keywords) but it doesn't. This seems like a Core Data bug to me.

两个谓词

[NSPredicate predicateWithFormat:@"NONE answers.correct == 1"]
[NSPredicate predicateWithFormat:@"NOT (ANY answers.correct == 1)"]

返回相同的结果集

[NSPredicate predicateWithFormat:@"ANY answers.correct != 1"]

-com.apple.CoreData.SQLDebug 3 并检查SQL select语句。

as can be seen by setting the launch argument -com.apple.CoreData.SQLDebug 3 and inspecting the SQL select statements.

我可以找到使用 SUBQUERY

[NSPredicate predicateWithFormat:@"SUBQUERY(answers, $a, $a.correct == 1).@count == 0"]

获取所有没有正确== 1的答案的对象。

to get all objects that do not have any "answer" with "correct == 1".

这篇关于NSPredicate使用NONE聚合操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆