Nspredicate predicate summary array filtering fuzzy matching

Source: Internet
Author: User
Tags logical operators vars

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. 1. Create a nspredicate (equivalent to create a filter condition)
    2. Nspredicate *predicate = [nspredicate predicatewithformat:@ "filter condition"];
    3. 2. Determine whether the specified object meets nspredicate created filter conditions
    4. [predicate Evaluatewithobject:person];
    5. 3. Filter out eligible objects (return all eligible objects)
    6. Nsarray *persons = [array filteredarrayusingpredicate:predicate];
Example: (The following is pseudo-code, only to explain the problem) [OBJC]View Plaincopy
  1. 1. Create a Person object first
  2. @interface person:nsobject{
  3. NSString *name;
  4. int age;
  5. }
  6. 2. Create an array to add multiple person objects to the array type
  7. Nsarray *array=[nsarray arraywithobjects:person1,person2,person3,person4,...,Nil];
  8. 3. Use Nspredicate to filter the person of the array type
  9. A. Finding the person with an array of age less than 2 0
  10. Nspredicate *predicate = [nspredicate predicatewithformat:@ "Age < 20"];
  11. for (person*person in array) {
  12. if ([predicate Evaluatewithobject:person]) { //Determines whether the specified object satisfies
  13. //........................  
  14. }
  15. }
  16. 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
    1. Nspredicate *predicate = [nspredicate predicatewithformat:@ "Age > 20"];
    2. Nspredicate *predicate = [nspredicate predicatewithformat:@ "name > ' abc ' && Age > 10"];
    3. Nspredicate *predicate = [nspredicate predicatewithformat:@ "name > ' abc ' OR Age > 10"];
[OBJC]View Plaincopy
    1. <span style="font-family:arial, Helvetica, Sans-serif;" ></span><p class="P1" >
    2. </p>

2.IN

[OBJC]View Plaincopy
    1. Nspredicate *predicate = [nspredicate predicatewithformat:@ "name in {' abc ', ' Def ', ' 123 '}"];

3. Start with XX--beginswith [OBJC]View Plaincopy
    1. Nspredicate *predicate = [nspredicate predicatewithformat:@ "name Beginswith ' N '"]; Name the person with the N
[OBJC]View Plaincopy
    1. </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
    1. 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
    1. 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
    1. Nsarray *array=[nsarray arraywithobjects: @ "abc", @ "def", @ "Ghi",@ "JKL", nil nil];
    2. Nspredicate *pre = [nspredicate predicatewithformat:@ "self== ' abc '"];
    3. Nsarray *array2 = [array filteredarrayusingpredicate:pre];

8. Regular expressions [OBJC]View Plaincopy
  1. (8.) Regular expression:
  2. Nspredicate uses matches to match regular expressions, and the notation for regular expressions takes international components
  3. The regular syntax for the for Unicode (ICU).
  4. Cases:
  5. NSString *regex = @ "^a.+e$";   A character that begins with a and ends with E.
  6. Nspredicate *pre= [nspredicate Predicatewithformat:@ "Self MATCHES%@", regex];
  7. if ([Pre evaluatewithobject: @ "Apple"]) {
  8. printf ("yes\n");
  9. }else{
  10. printf ("no\n");
  11. }


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.