Nspredicate is used for query and filtering.
In SQL, where is usually used as the query condition, but nspredicate can be used as the query condition in coredata.
Nspredicate can not only be used with fetchrequest in coredata. It can also be used with nsarray.
Keywords and conditions supported in nspredicate:
1. >,<,>=, <=, = comparison operator.
For example:
Nspredicate * qcondition = [nspredicate predicatewithformat: @ "salary >=10000"];
2. String operations (including): beginswith, endswith, and contains
For example:
@ "Employee. Name beginswith [cd] 'lil'" // an employee surnamed Li
@ "Employee. Name endswith [c] 'meng'" // an employee whose dream ends
@ "Employee. name contains [d] 'zong'" // an employee with the word "Zong"
Note: [c] Is case insensitive. [d] It is case insensitive and does not have an accent symbol. [cd] Is case insensitive and does not have a pronunciation symbol.
3. Range: In, bwteen
For example:
@ "Salary bwteen }"
@ "Em_dept in 'developing '"
4. itself: self, which only works for character arrays.
For example:
Nsarray * test = [nsarray arraywithobjects: @ "Guangzhou", @ "Beijing", @ "Shanghai", nil];
@ "Self = 'beijing '"
5. wildcard: Like
Like usage? Represents a single character. * represents multiple characters. It can also be used with C or D.
For example:
@ "Car. name like '? He? '"// Four characters in the middle: He
@ "Car. name like '* JP'" // end with JP
6. Regular Expression: matches
For example:
Nsstring * RegEx = @ "^ e. + e $"; // a string of characters starting with E and ending with E.
Nspredicate * pre = [nspredicate predicatewithformat: @ "Self matches % @", RegEx];
If ([pre evaluatewithobject: @ "employee"]) {
Nslog (@ "matches yes ");
} Else {
Nslog (@ "matches no ");
}
7. logical operators: And, or, not
For example:
@ "Employee. Name = 'john' and employee. Age = 28"
8. placeholders:
Nspredicate * pretemplate = [nspredicate predicatewithformat: @ "name = $ name"];
Nsdictionary * DIC = [nsdictionary dictionarywithobjectsandkeys:
@ "Name1", @ "name", nil];
Nspredicate * pre = [pretemplate predicatewithsubstitutionvariables: DIC];
Placeholders are the keys in the dictionary object. Therefore, you can have multiple placeholders, as long as the keys are different.
Filter Arrays:
For example:
Nsmutablearray * carscopy = [carsmutablecopy];
[Carscopyfilterusingpredicate: predicate]; // The new array is composed of filterusingpredicate and nsmutablearray.
Nslog (@ "% @", carscopy );
Predicate = [nspredicatepredicatewithformat: @ "engine. horsepower> % d", 50];
Results = [Cars filteredarrayusingpredicate: predicate];
Nslog (@ "% @", results );
Coredata Summary 3 nspredicate condition query or excessive consideration