IOS Predicate-nspredicate

Source: Internet
Author: User
Cocoa provides nspredicate to specify filtering conditions. A predicate is a function that represents a real value in a computer,
It is a bit like an SQL query condition. It is mainly used to sort objects that meet the conditions from the collection.
It is used for string regular match. First, let's look at a very simple example to understand the predicates.
# Import <Foundation/Foundation. h>
@ Interface person: nsobject {
Int PID;
Nsstring * Name;
Float height;
}
-(Void) setpid: (INT) PID;
-(Void) setname: (nsstring *) Name;
-(Void) setheight: (float) height;
-(INT) PID;
-(Nsstring *) Name;
-(Float) height;
@ End
@ Implementation person
-(Void) setpid: (INT) P {
PID = P;
}
-(Void) setname: (nsstring *) n {
[N retain];
[Name Release];
Name = N;
}
-(Void) setheight: (float) h {
Height = h;
}
-(INT) PID {
Return PID;
}
-(Nsstring *) name {
Return name;
}
-(Float) height {
Return height;
}
-(Void) dealloc {
[Name Release];
[Super dealloc];
}
@ End
Int main (INT argc, const char * argv []) {
NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];
// Instantiate three persons and put them in an array.
Nsmutablearray * array = [nsmutablearray arraywithcapacity: 5];
Person * person1 = [[person alloc] init];
[Person1 setpid: 1];
[Person1 setname: @ "name1"];
[Person1 setheight: 168];
[Array addobject: person1];
Person * person2 = [[person alloc] init];
[Person2 setpid: 2];
[Person2 setname: @ "name2"];
[Person2 setheight: 178];
[Array addobject: person2];
Person * person3 = [[person alloc] init];
[Person3 setpid: 3];
[Person3 setname: @ "name3"];
[Person3 setheight: 188];
[Array addobject: person3];
// Create a predicate with the condition pid> 1 and height <188.0. In fact, the predicates are also based on KVC, that is
If the PID is in the member variable XXX of person, it must be written as XXX. PID> 1.
Nspredicate * pre = [nspredicate predicatewithformat:
@ "Pid> 1 and height <188.0"];
Int I = 0;
For (; I <[array count]; I ++ ){
Person * person = [array objectatindex: I];
// Traverse the array and output the name of the person that meets the predicate conditions.
If ([pre evaluatewithobject: person]) {
Nslog (@ "% @", [person name]);
}
}
[Person1 release];
[Person2 release];
[Person3 release];
[Pool release];
Return 0;
}
The shell window output is as follows:
16:51:18. 382 predicate [2400] name2
We can see that the class method predicatewithformat: (nsstring *) format is used to create a predicate.
Similar to the SQL where condition. In addition, the format parameter is similar to the format template of nslog.
188.0 is the passed parameter. You can write it as follows:
@ "Pid> % d and height <% F", 1,188.0
(1) logical operators: And, or, not
Returns the sum, or, or, not of these operators.
(2) range operators: between, in
Example:
@ "PID between {1, 5 }"
@ "Name in {'name1', 'name2 '}"
(3.) placeholder:
Nspredicate * pretemplate = [nspredicate predicatewithformat: @ "name = $ name"];
Nsdictionary * DIC = [nsdictionary dictionarywithobjectsandkeys:
@ "Name1", @ "name", nil];
Nspredicate * pre = [pretemplate predicatewithsubstitutionvariables: DIC];
A placeholder is the key in the Field object, so you can have multiple placeholders, as long as the key is different.
(4) Fast filtering array:
Previously, we used predicates to determine whether the objects in the array match one by one. In fact, the array itself has a more convenient method,
Filter out a new array that matches the predicates.
Nspredicate * pre = [nspredicate predicatewithformat: @ "pid> 1"];
Nsmutablearray * arraypre = [array filteredarrayusingpredicate: Pre];
Nslog (@ "% @", [[arraypre objectatindex: 0] Name]);
(5) string operators:
Beginswith, endswith, and contains indicate whether to start, end, and contain a string.
They can be used with C and D to indicate whether uppercase or lowercase letters are ignored, and whether accent letters are ignored (the tone is above the letter ).
Example:
@ "Name beginswith [cd] 'hes '"
Determines whether the name starts with "he" and ignores uppercase and lowercase letters and accent letters.
(6.) Like OPERATOR:
Like usage? Represents a single character. * represents multiple characters. It can also be used with C or D.
Example:
@ "Name like '??? Er * '"matches paper plane.
(7.) self:
Objects are put in the preceding array. If all strings (or other non-attribute types) are put in the array
How do I write predicates? Here we use self.
Example:
Nsarray * arrays = [nsarray arraywithobjects: @ "apple", @ "google", @ "Mircosoft", nil];
Nspredicate * pre2 = [nspredicate predicatewithformat: @ "Self = 'apple'"];
(8.) regular expression:
Nspredicate uses matches to match regular expressions. The regular expression is written using international components.
For Unicode (ICU) Regular syntax.
Example:
Nsstring * RegEx = @ "^ A. + e $"; // a character that starts with a and ends with E.
Nspredicate * pre = [nspredicate predicatewithformat: @ "Self matches % @", RegEx];
If ([pre evaluatewithobject: @ "apple"]) {
Printf ("Yes \ n ");
} Else {
Printf ("NO \ n ");
}
Related Article

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.