Nspredicate is used to specify filtering criteria, primarily for sorting out eligible objects from the collection, or for regular matching of strings. Introduction to common methods of nspredicate
[OBJC]View Plaincopy
- 1. Create a nspredicate (equivalent to create a filter condition)
- Nspredicate *predicate = [nspredicate predicatewithformat:@ "filter condition"];
- 2. Determine whether the specified object meets nspredicate created filter conditions
- [predicate Evaluatewithobject:person];
- 3. Filter out eligible objects (return all eligible objects)
- Nsarray *persons = [array filteredarrayusingpredicate:predicate];
Example: (The following is pseudo-code, only to explain the problem)
[OBJC]View Plaincopy
- 1. Create a Person object first
- @interface person:nsobject{
- NSString *name;
- int age;
- }
- 2. Create an array to add multiple person objects to the array type
- Nsarray *array=[nsarray arraywithobjects:person1,person2,person3,person4,...,Nil];
- 3. Use Nspredicate to filter the person of the array type
- A. Finding the person with an array of age less than 2 0
- Nspredicate *predicate = [nspredicate predicatewithformat:@ "Age < 20"];
- for (person*person in array) {
- if ([predicate Evaluatewithobject:person]) { //Determines whether the specified object satisfies
- //........................
- }
- }
- Nsarray *persons = [array filteredarrayusingpredicate:predicate]; Get all person with age less than 20
Use method mainly in these steps, the following are some common nspredicate conditions 1. Logical operation symbols >, <, =, >=, <= can be used here operators can also be used with logical operators,&&, | | , and, OR predicates are case insensitive
[OBJC]View Plaincopy
- Nspredicate *predicate = [nspredicate predicatewithformat:@ "Age > 20"];
- Nspredicate *predicate = [nspredicate predicatewithformat:@ "name > ' abc ' && Age > 10"];
- Nspredicate *predicate = [nspredicate predicatewithformat:@ "name > ' abc ' OR Age > 10"];
[OBJC]View Plaincopy
- <span style="font-family:arial, Helvetica, Sans-serif;" ></span><p class="P1" >
- </p>
2.IN
[OBJC]View Plaincopy
- Nspredicate *predicate = [nspredicate predicatewithformat:@ "name in {' abc ', ' Def ', ' 123 '}"];
3. Start with XX--beginswith
[OBJC]View Plaincopy
- Nspredicate *predicate = [nspredicate predicatewithformat:@ "name Beginswith ' N '"]; Name the person with the N
[OBJC]View Plaincopy
- </pre>4. End With XX--endswith</div><div></div><div><pre code_snippet_id=" 163702 "Snippet_file_name=" blog_20140120_8_9862346 "name=" code " class=" OBJC ">nspredicate * predicate = [Nspredicate predicatewithformat:@ "name ENDSWITH ' N '"]; Name the person that ends with n
5. Included--Contains
[OBJC]View Plaincopy
- Nspredicate *predicate = [nspredicate predicatewithformat:@ "name CONTAINS ' N '"]; <span style=" Font-family:arial, Helvetica, Sans-serif; >//name person</span> containing ' n '
6. Fuzzy Query--like
[OBJC]View Plaincopy
- Nspredicate *predicate = [nspredicate predicatewithformat:@ "name like ' *n* '"]; <span style= "Color:rgb (51, 51, 51); font-family:arial; font-size:14.44444465637207px; line-height:26px; " >* represents 0 or more characters </span>
7. All of the above is the property of the object match, if the array is a string how to match--self
[OBJC]View Plaincopy
- Nsarray *array=[nsarray arraywithobjects: @ "abc", @ "def", @ "Ghi",@ "JKL", nil nil];
- Nspredicate *pre = [nspredicate predicatewithformat:@ "self== ' abc '"];
- Nsarray *array2 = [array filteredarrayusingpredicate:pre];
8. Regular expressions
[OBJC]View Plaincopy
- (8.) Regular expression:
- Nspredicate uses matches to match regular expressions, and the notation for regular expressions takes international components
- The regular syntax for the for Unicode (ICU).
- Cases:
- NSString *regex = @ "^a.+e$"; A character that begins with a and ends with E.
- Nspredicate *pre= [nspredicate Predicatewithformat:@ "Self MATCHES%@", regex];
- if ([Pre evaluatewithobject: @ "Apple"]) {
- printf ("yes\n");
- }else{
- printf ("no\n");
- }
Remark: The above is my humble opinion, if there is no place, please the big points to correct, thank you!!
Nspredicate predicate summary array filtering fuzzy matching