Coredata Summary 3 nspredicate condition query or excessive consideration

Source: Internet
Author: User

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

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.