Use of predicates in iOS

Source: Internet
Author: User

Cocoa provides a class nspredicate class that is primarily used to specify the condition of the filter, which can accurately describe the required conditions, filter each object through predicates, and determine if the condition matches. predicates represent functions that calculate true or false values. A database framework encapsulated in Cocoa Cocoadata also uses predicates in queries (including fuzzy queries), with a brief introduction to the use of predicates and their rules:

1, create a predicate with Predicatewithformat

    • Conditions

NSString *predicatestring [email protected] "name== ' Herbie '"

NSString *predicatestring [email protected] "Engine.horsepower > 150" property values

NSString *predicatestring [email protected] "(Engine.horsepower >) and (Engine.horsepower < 200)"; operators can be used

NSString *predicatestring [email protected] "Engine.horsepower between {50, 200}"; You can use the between keyword here. Same as the above effect

NSString *predicatestring [email protected] "name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}";

NSString *predicatestring [email protected] "name Beginswith ' bad '; string operator BEGINSWITH,ENDSWITH,CONTAINS[C][D][CD]

Beginswith: Checks whether a string starts with another string.

ENDSWITH: Checks whether a string ends with another string.

CONTAINS: Checks whether a string is inside another string.

[c] Case insensitive [d] does not distinguish between pronounced symbols and no accent marks [CD] are neither case sensitive nor pronounced.

    • Create predicate

Nspredicate *predicate=[nspredicate Predicatewithformat:predicatestring]

results = [cars filteredarrayusingpredicate:predicate];

NSLog (@ "%@", results);

    • Use of like

predicate = [nspredicate predicatewithformat:@ "name LIKE[CD] ' *er* '"];//* represents a wildcard like also accepts [CD].

results = [cars filteredarrayusingpredicate:predicate];

NSLog (@ "%@", results);

predicate = [nspredicate predicatewithformat:@ "name LIKE[CD] '??? Er* ' "];//? matches only one character and can also accept [CD].

results = [cars filteredarrayusingpredicate:predicate];

NSLog (@ "%@", results);

Use of predicates in iOS

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.