Predicate: nspredicate
1, Overview:
Used to set the condition of the specified filter, the popular understanding is to set the conditions of the selection, and then apply the condition to the specified object
2, Usage:
A, set the predicate first.
Nspredicate *predicate=[nspredicate predicatewithformat: "name== ' Herbie '"];
It assigns the "name== ' Herbie '" predicate string to the predicate, or the key path if the text block is not using single quotation marks, or a block of text if it is referenced.
b, the predicate is calculated from an object.
BOOL match=[predicate Evaluatewithobject:car] (here is the calculation of Yes or no)
Or
Set the predicate and calculate as follows:
Nspredicate *predicate=[nspredicate predicatewithformat:@ "engine.horsepower>150";
Match=[predicate Evaluatewithobject:car];
3, extension usage
A, use for Nsarray.
Nsarray *results;
Results=[cars Filteredarrayusingpredicate:predicate];
b, the use of Nsmutabarray is similar to a usage.
4, progressive usage
A, use the format specifier.
Peel the condition settings out of the conditional expression.
Predicate=[nspredicate predicatewithformat:@ "engine.horsepower>%d", 150];
Predicate=[nspredicate predicatewithformat:@ "name==%@", @ "Herbie"]; If you want to use%@ as a condition after the equals sign, you should use @ "name== '%@ '", @ "Herbie "
b, put the variable name into the string dictionary, similar to the environment variable
The temporary predicate is constructed first: Predicatetemplate=[nspredicate predicatewithformat:@ "name== $NAME"];
Re-construct a dictionary: nsdictionary *vardict=[nsdictionary dictionarywithobjectandkeys:
@ "Herbie", @ "NAME", nil]
Construct new predicate: Predicate=[predicatetemplate predicatewithsubstitutionvariables:vardict];
Or as follows:
Predicatetemplate=[nspredicate Predicatewithformat: @engine. Horsepower > $POWER "];
Nsdictionary *vardict=[nsdictionary Dictionarywithobjectandkeys:
[NSNumber numberwithint:150],@ "POWER", nil];
Predicate=[predicatetemplate Predicatewithsubstitutionvariables:vardict];
C, using operators
C1, Comparison and logical operators >,>=,=>,<=,<,<=,=<,!=,<>, (), and,or,not,&&,| |,!
Predicate=[nspredicate predicatewithformat:@ "(engine.horsepower>50) and (Engine.horsepower <200)"];
Results=[cars Filterdarrayusingpredicate:predicate];
C2, array operator between,in
Nsarray *betweens=[nsarray Arraywithobject:
[NSNUMBERWITHINT:50]
[Nsnumberwithint:200],nil];
Predicate=[nspredicate Predicatewithformat:
@ "Engine.horsepower between%@", Betweens];
You can also use the methods of the preceding environment variables.
C3, use a string array as a predicate (using self).
For example, I have a string array: {"Herbie", "Snugs", "Badger", "Flap"}, to find it as a condition in cars.name.
Nsarray *names=[cars valueforkey:@ "name"];
Nspredicate *predicate=[nspredicate Predicatewithformat:
@ "Self in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}];
Results=[names Filteredarrayusingpredicate:predicate];
Remember this: Self can refer to a pair of images for predicate calculations. As follows:
Nsarray *names1=[nsarray arraywithobjects:
@ "Herbie", @ "Badger", @ "Judge", @ "Elvis", nil];
Nsarray *names2=[nsarray arraywithobjects:
@ "Judge", @ "Paper Car", @ "Badger", @ "Finto", nil];
Predicate=[nspredicate predicatewithformat:@ "Self in%@", name1];
Results=[names2 Filteredarrayusingpredicate:predicate];
C4, string operator
Beginwith,endwith,contains,
For example, use "Name Beginswith ' Bad '" to match "Badger"
Typically there are modifiers [C] that are not case-sensitive, [d] do not distinguish between pronounced symbols, [CDs] are case-insensitive, and do not distinguish between pronounced symbols, such as:
"Name BEGINSWITH[CD] ' HERB '"
C5,like operator
such as the predicate string: "Name like ' *er* '", "name as"??? Er* ' "
C6,matchs operator
The main use is regular expressions, but the computational overhead is very high.
Objective-c Diary-The Nspredicate