iOS: Reprint: iOS verb--nspredicate

Source: Internet
Author: User

iOS verb--nspredicateCategory: iOS apps 2013-02-19 17:24 6792 People read Comments (1) favorite reports Cocoa provides a nspredicate for specifying the filter condition, which is a function that represents the calculation of true and false values in the computer.
It's a bit like a SQL query condition, it's used primarily to sort out eligible objects from a collection, or you can
A regular match for the string. First, let's look at a very simple example that has a perception of 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[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Instantiate three person and put 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 predicates, conditions are pid>1 and height<188.0. In fact, predicates are also based on KVC, i.e.
The PID is in the member variable xxx of person, then write xxx.pid>1 here.
Nspredicate *pre = [Nspredicate predicatewithformat:
@ "pid>1 and height<188.0"];
int i=0;
for (; I<[array count];i++) {
Person *person=[array OBJECTATINDEX:I];
Iterates through the array, outputting the name of the person who conforms to the predicate condition.
if ([Pre Evaluatewithobject:person]) {
NSLog (@ "%@", [person name]);
}
}
[Person1 release];
[Person2 release];
[Person3 release];
[Pool release];
return 0;
}
The Shell window output looks like this:
2011-04-01 16:51:18.382 predicate[2400] Name2
We see the CREATE predicate using the class method Predicatewithformat: (nsstring*) Format,format something really
is similar to the Where condition of SQL. In addition, the parameter format is similar to the NSLog format template, if 1 and
188.0 is passed over the parameters, you can write the following form:
@ "pid>%d and height<%f", 1,188.0
(1.) Logical operators: And, OR, not
These operators calculate the result of a and, or, not.
(2.) scope operator: between, in
Cases:
@ "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];
The placeholder is the key in the Field object, so you can have multiple placeholders, as long as the key is different.
(4.) Quickly filter arrays:
Before we use predicates to determine whether an object in an array conforms to each other, in fact the array itself has a more convenient method,
Directly filters out a new array that conforms to the predicate.
Nspredicate *pre = [nspredicate predicatewithformat:@ "pid>1"];
Nsmutablearray *arraypre=[array Filteredarrayusingpredicate:pre];
NSLog (@ "%@", [[Arraypre objectatindex:0] name]);
(5.) string operator:
Beginswith, ENDSWITH, CONTAINS, respectively, indicate whether to start, end, or include a string.
They can be used in conjunction with C and D to indicate whether to ignore the case and whether to ignore accented letters (there is a tone label above the letters).
Cases:
@ "name BEGINSWITH[CD] ' He '"
Determines whether name begins with the He and ignores case, ignoring accented letters.
(6.) Like operator:
Like use? represents a character, * represents more than one character, or can be used with C, D.
Cases:
@ "Name like '??? Er* ' "matches the paper Plane.
(7.) Self:
All of the preceding arrays are objects, and if the arrays are all strings (or other types with no attributes), the
How do you write predicates? Here we use self.
Cases:
Nsarray *arrays=[nsarray arraywithobjects: @ "Apple", @ "Google", @ "MircoSoft", nil];
Nspredicate *pre2 = [nspredicate predicatewithformat:@ "self== ' Apple '"];
(8.) Regular expression:
Nspredicate uses matches to match regular expressions, and the notation for regular expressions takes international components
The regular syntax for the for Unicode (ICU).
Cases:
NSString *regex = @ "^a.+e$";//a character that begins with a and ends with E.
Nspredicate *pre= [nspredicate predicatewithformat:@ "Self MATCHES%@", regex];
if ([Pre evaluatewithobject: @ "Apple"]) {
printf ("yes\n");
}else{
printf ("no\n");
}

































































































































iOS: Reprint: iOS verb--nspredicate

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.