有没有一种方法可以“反转”NSPredicate?

8
假设我有这个谓词:
NSPredicate *filter = [NSPredicate predicateWithFormat:@"%K == NO", @"someAttribute"];

我希望你能将该谓词反转为:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"%K == YES", @"someAttribute"];

有没有一种方法可以不创建新的谓词来完成这个操作?

1
纯逻辑的“反转”公式应该是%K != NO - holex
1个回答

11

你无法修改现有的NSPredicate。但是你可以非常容易地创建一个新的谓词,该谓词是filter的“反向”(更准确地说是否定):

NSPredicate *notFilter = [NSCompoundPredicate notPredicateWithSubpredicate:filter];

请参阅+[NSCompoundPredicate notPredicateWithSubpredicate:]文档


非常感谢,正是我所寻找的。 - shannoga

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接