美文网首页Predicate
NSCompoundPredicate的使用

NSCompoundPredicate的使用

作者: 想聽丿伱說衹愛我 | 来源:发表于2020-09-09 10:52 被阅读0次

版本:iOS13.7

一、简介

NSCompoundPredicate是复合谓词,通过多个谓词进行AND、OR、NOT组合来评估对象。
NSCompoundPredicate是NSPredicate的子类,点击NSPredicate的使用查看NSPredicate的用法,以便更容易理解NSCompoundPredicate。

二、NSCompoundPredicate的API

@interface NSCompoundPredicate : NSPredicate

//通过多个谓词创建一个复合谓词
//type 复合类型 详见说明1
//subpredicates 谓词数组
- (instancetype)initWithType:(NSCompoundPredicateType)type 
                subpredicates:(NSArray<NSPredicate *> *)subpredicates;
//初始化方法,一般使用上面的方法
- (nullable instancetype)initWithCoder:(NSCoder *)coder;

//复合类型 只读 详见说明1
@property (readonly) NSCompoundPredicateType compoundPredicateType;
//复合谓词的谓词数组 只读
@property (readonly, copy) NSArray *subpredicates;

//简易复合谓词的初始化方法 此时NSCompoundPredicateType为NSAndPredicateType
//相当于[[NSCompoundPredicate alloc] initWithType:NSAndPredicateType subpredicates:subpredicates]
+ (NSCompoundPredicate *)andPredicateWithSubpredicates:(NSArray<NSPredicate *> *)subpredicates;
//简易复合谓词的初始化方法 此时NSCompoundPredicateType为NSOrPredicateType
+ (NSCompoundPredicate *)orPredicateWithSubpredicates:(NSArray<NSPredicate *> *)subpredicates;
//简易复合谓词的初始化方法 此时NSCompoundPredicateType为NSNotPredicateType
+ (NSCompoundPredicate *)notPredicateWithSubpredicate:(NSPredicate *)predicate;

@end
  • 说明1
typedef NS_ENUM(NSUInteger, NSCompoundPredicateType) {
    //给单个谓词表达式添加NOT,SELF CONTAINS 'string'变成NOT SELF CONTAINS 'string'
    NSNotPredicateType = 0, 
    //多个谓词表达式之间添加AND
    NSAndPredicateType,
    //多个谓词表达式之间添加OR
    NSOrPredicateType,
};

三、应用

    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF > 10"];
    NSLog(@"predicate1 = %@", [predicate1 predicateFormat]);
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF < 50"];
    NSLog(@"predicate2 = %@", [predicate2 predicateFormat]);
    NSCompoundPredicate *compound = [[NSCompoundPredicate alloc] initWithType:NSAndPredicateType 
                                      subpredicates:@[predicate1, predicate2]];
    NSLog(@"compound = %@", [compound predicateFormat]);
    success = [compound evaluateWithObject:@20];
    NSLog(@"%@", @(success));
输出:
predicate1 = SELF > 10
predicate2 = SELF < 50
compound = SELF > 10 AND SELF < 50
1

将两个谓词组合起来,最终复合谓词表达式变成SELF > 10 AND SELF < 50

相关文章

  • NSCompoundPredicate的使用

    版本:iOS13.7 一、简介 NSCompoundPredicate是复合谓词,通过多个谓词进行AND、OR、N...

  • MacOS开发笔记20-NSCompoundPredicate

    NSCompoundPredicate需要注意个数,如果个数小于2个,不要使用NSCompoundPredicat...

  • 定义谓语

    谓语用NSPredicate对象来代表三个子类: NSCompoundPredicate NSCompoundPr...

  • iconfont的使用(下载使用)

    1、下载文件 2、在生命周期中引入项目 beforeCreate () { var domModule = ...

  • Gson的使用--使用注解

    Gson为了简化序列化和反序列化的过程,提供了很多注解,这些注解大致分为三类,我们一一的介绍一下。 自定义字段的名...

  • 记录使用iframe的使用

    默认记录一下----可以说 这是我第一次使用iframe 之前都没有使用过; 使用方式: 自己开发就用了这几个属...

  • with的使用

    下面例子可以具体说明with如何工作: 运行代码,输出如下

  • this的使用

    什么是this? this是一个关键字,这个关键字总是返回一个对象;简单说,就是返回属性或方法“当前”所在的对象。...

  • this的使用

    JS中this调用有几种情况 一:纯粹的函数调用 这是函数的最通常用法,属于全局性调用,因此this就代表全局对象...

  • ==的使用

    积累日常遇到的编码规范,良好的编码习惯,持续更新。。。 日常使用==用于判断的时候,习惯性将比较值写前面,变量写后...

网友评论

    本文标题:NSCompoundPredicate的使用

    本文链接:https://www.haomeiwen.com/subject/cvxeektx.html