有没有办法"逆转"NSPredicate

sha*_*oga 6 nspredicate ios

让我们说我有这个谓词:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"%K == NO", @"someAttribute"];
Run Code Online (Sandbox Code Playgroud)

我希望将该谓词改为:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"%K == YES", @"someAttribute"];
Run Code Online (Sandbox Code Playgroud)

有没有办法在不创建新谓词的情况下执行此操作.

rob*_*off 9

您无法修改现有的NSPredicate.但是你可以很容易地创建一个新的谓词:"反向"(或更确切地说,否定)filter:

NSPredicate *notFilter = [NSCompoundPredicate notPredicateWithSubpredicate:filter];
Run Code Online (Sandbox Code Playgroud)

请参阅+[NSCompoundPredicate notPredicateWithSubpredicate:]文档.