NSPredicate implement data filtering

A: basic grammar

1. What is NSPredicate apple official documents written like this: at The NSPredicate class IS the DEFINE Used to Used to constrain the Logical Conditions either A Search for A FETCH or for in-Memory Filtering. (NSPredicate class is defined by logical conditions to constrain or get memory the search filters.)

2. The basic syntax (case may be, but is recommended to use more capital to represent these reserved words) as long as we use the verb (NSPredicate) are required to define the predicate predicate expression, and this expression must be a BOOL return value. Predicate expression constituted by expression, operator and value. The SELF = 123

(1) comparison == = no assignment and determining whether said is equal to a

(2)> = <=> <! = <> In addition determines whether or between BETWEEN

/ * Values ​​meet the condition range between SELF> 1 && SELF <10

​ OR   Or a logic output to meet

The NOT! Non-inverted logic

​ */

// between
  int testNumber = 4;

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN {1, 10}"];

  if ([predicate evaluateWithObject:[NSNumber numberWithInteger:testNumber]]) {

    NSLog(@"testString:%d", testNumber);

  } else {

    NSLog(@"不符合条件");

  } 

(3) String comparison

BEGINWITH check whether a character in a specified string beginning

eg: if compared to the beginning of a BEGINSWITH 'a'

ENDSWITH with () ending

CONTAINS contains

LIKE check whether a string matches the specified string template, then it can be followed? And two wildcards (? Represents one character  on behalf of any number of characters)

 	eg: "name LIKE'?ac*'"  表示name的第2 3个字符是ac则返回YES "name LIKE'*ac*'" 表示name包含ac则返回YES 

MATCHES check whether a string matches the specified regular expression

(4) set operator

ANY, SOME: collection element satisfies any one condition, returns YES.

ALL: All the elements are set to meet the conditions, before returning YES.

NONE: no elements meet the set conditions return YES. Such as: NONE person.age <18, represents age person collection of all elements> = 18 when only return YES.

IN: equivalent to the SQL statement in the IN operator, only if the left expression or set of values ​​appear on the right will return YES.

array [index]: Returns the index of the element array index array

array[FIRST]:返回array数组中第一个元素

array[LAST]:返回array数组中最后一个元素

array[SIZE]:返回array数组中元素的个数

Two, NSPredicate usage

(1) defined predicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN {1, 10}"];
(2) (heavy and difficult module) using a filter set predicate

This part is the focus we need to know, because from here we can see the real power of the predicate

In fact, the predicate itself represents a logical condition, results returned after calculating the value of the predicate is always type BOOL. The predicate is the most commonly used features of the collection filter.

******** focus **********

When a program uses a filter predicate collection element, the program will automatically traverses its elements, and the element value is calculated based on a set of predicates, when the elements of the collection evaluates the predicate and returns YES, this element will be retained.

​ ***********************

ps: Note that the program will automatically cycles through its elements, it will automatically return value traversed after YES is reassembled into a collection returned.

 

  NSArray provides a method using a set of predicates to filter

  New set using the specified predicate filter NSArray set, return the right elements of: (NSArray <ObjectType> *) filteredArrayUsingPredicate -: (NSPredicate *) predicate

 NSMutableArray provides a method using a set of predicates to filter
  - (void) filterUsingPredicate: (NSPredicate *) predicate: using the specified predicate filter NSMutableArray, excluding collection element ineligible

 NSSet provides a method using a set of predicates to filter
  - (NSSet <ObjectType> *) filteredSetUsingPredicate: (NSPredicate *) predicate NS_AVAILABLE (10_5, 3_0): role in the same manner NSArray

  
  NSMutableSet provides a method using a set of predicates to filter
  - (void) filterUsingPredicate: (NSPredicate *) predicate NS_AVAILABLE (10_5, 3_0): the role with NSMutableArray Methods.

As can be seen from the above description, the difference between the filter predicate immutable set and variable set is: filtering immutable set, return the set of qualifying a new set of elements; filtration variable set, no return value, excluding does not directly meet the conditions set elements

 

Guess you like

Origin www.cnblogs.com/lovemargin/p/11880751.html