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表示对象自身