NSPredicate with a !=?

北城以北 提交于 2019-12-02 09:20:56
Martin R
[NSPredicate predicateWithFormat:@"ANY persons != %@", fred]

finds all objects that are related to any person other that Fred. What you want is

[NSPredicate predicateWithFormat:@"NOT(ANY persons = %@)", fred]

and this should return all objects that are not related to Fred.

However, there seems to be a Core Data bug that "NOT ANY" or "NONE" do not work correctly in a predicate, compare NSPredicate Aggregate Operations with NONE. The workaround is to use a SUBQUERY:

[NSPredicate predicateWithFormat:@"SUBQUERY(persons, $p, $p == %@).@count == 0", fred]

you need to ask first in Class Boundary if there is a person called Fred that has boundaries. It should be something like:

NSFetchRequest* fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Boundary"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"ANY persons.name != %@", person.name];

That will fetch all the boundaries that Fred doesn't have.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!