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
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.
Nspredicate *predicate=[nspredicate Predicatewithformat:predicatestring]
results = [cars filteredarrayusingpredicate:predicate];
NSLog (@ "%@", results);
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