zoukankan      html  css  js  c++  java
  • Objective C 总结(十二):NSPredicate

    谓词定义了真值条件,对象通过谓词进行筛选,判断是否与条件相匹配。

    @interface NSPredicate : NSObject <NSCoding, NSCopying> {
        void *_reserved;
    }
    
    // Parse predicateFormat and return an appropriate predicate
    + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat argumentArray:(NSArray *)arguments;
    + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...;
    + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat arguments:(va_list)argList;
    
    + (NSPredicate *)predicateWithValue:(BOOL)value;    // return predicates that always evaluate to true/false
    
    
    + (NSPredicate*)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block; 
    
    
    - (NSString *)predicateFormat;    // returns the format string of the predicate
    
    - (NSPredicate *)predicateWithSubstitutionVariables:(NSDictionary *)variables;    // substitute constant values for variables
    
    - (BOOL)evaluateWithObject:(id)object;    // evaluate a predicate against a single object
    
    - (BOOL)evaluateWithObject:(id)object substitutionVariables:(NSDictionary *)bindings; // single pass evaluation substituting variables from the bindings dictionary for any variable expressions encountered
    
    @end
    
    @interface NSArray (NSPredicateSupport)
    - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate;    // evaluate a predicate against an array of objects and return a filtered array
    @end
    
    @interface NSMutableArray (NSPredicateSupport)
    - (void)filterUsingPredicate:(NSPredicate *)predicate;    // evaluate a predicate against an array of objects and filter the mutable array directly
    @end
    
    
    @interface NSSet (NSPredicateSupport)
    - (NSSet *)filteredSetUsingPredicate:(NSPredicate *)predicate;    // evaluate a predicate against a set of objects and return a filtered set
    @end
    
    @interface NSMutableSet (NSPredicateSupport)
    - (void)filterUsingPredicate:(NSPredicate *)predicate;    // evaluate a predicate against a set of objects and filter the mutable set directly
    @end

    格式说明

    1. [NSPredicate predicateWithFormat: @"firstName==%@", @"bob"];
    2. [NSPredicate predicateWithFormat: @"%K==%@", @"firstName",@"bob"];
    3. NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat: @"firstName==$FirstName"];
      NSDictionary *varDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"bob", "firstName", nil];
      NSPredicate *predicate = [predicateTemplate predicateWithSubstitutionVariables: varDictionary];
    4. BOOL isMatch = [predicate evaluateWithObject: somePerson]; // 进行计算

    运算符

    >:, >=, <:, <=, ==, !=, &&, ||, !

    predicate = [NSPredicate predicateWithFormat: @"firstName != 'bob'"];

    BETWEEN, IN,

    [NSPredicate predicateWithFormat: @"height BETWEEN (160, 200)"];
    [NSPredicate predicateWithFormat: @"firstName in {'bob', 'john'}"];

    BEGINSWITH, ENDSWITH, CONTAINS,字符串匹配也可以应用一些比较修饰符

    [c]不区分大小写[d]不区分发音符号[cd]一起应用

    [NSPredicate predicateWithFormat: @"firstName BEGINSWITH[cd] 'bo'"];

    LIKE, ?匹配单字母,*匹配多字母,也可以应用[cd]修饰

    [NSPredicate predicateWithFormat: @"firstName LIKE '*bo?'"];

    SELF表示对象自身

  • 相关阅读:
    Linux下zip命令使用
    docker镜像发布到阿里云镜像仓库
    基于官方镜像定制php-fpm容器
    docker-compose部署开发环境
    docker安装discuz!Q
    从零开始实现简单 RPC 框架 4:注册中心
    从零开始实现简单 RPC 框架 3:配置总线 URL
    从零开始实现简单 RPC 框架 2:扩展利器 SPI
    从零开始实现简单 RPC 框架 1:RPC 框架的结构和设计
    文本分类(六):pytorch实现DPCNN
  • 原文地址:https://www.cnblogs.com/iprogrammer/p/3247503.html
Copyright © 2011-2022 走看看