Nspredicate: Predicates
The specific use of nspredicate should be filtered, similar to filter conditions.
first, the creation of Nspredicate:Nspredicate *predicate = [nspredicate predicatewithformat:@ "Age > 28"]; Filter condition Predicatewithformat: For class methods, we do not need to manage memory.the main methods of operation in Nspredicate
1. Comparison operators >, <, = =, >=, <=,! =
Example: @ "number >= 99"
2, logical operators: and, or, not these operators calculate and, or, not the result.
3. Range operator: In, between
Example: @ "number between {1,5}"
@ "address in {' Shanghai ', ' Nanjing '}"
4. String itself: Self
Example: @ "self = = ' APPLE '"
5. String correlation: Beginswith, ENDSWITH, CONTAINS
Example: @ "name CONTAIN[CD] ' ang '"//contains a string
@ "name Beginswith[c] ' sh '"//start with a string
@ "name Endswith[d] ' ang '"//End with a string
NOTE: [C] is not case sensitive, [d] does not differentiate between pronounced symbols without accents, [CD] is neither case-sensitive, nor does it distinguish between pronounced symbols.
6. Wildcard characters: Like
Example: @ "name LIKE[CD] ' *er* '"//* represents a wildcard, like also accepts [CD].
@ "name LIKE[CD] '??? Er* ' "
7. Regular expression: MATCHES
Example: NSString *regex = @ "^a.+e$"; Start with a, E end
@ "Name MATCHES%@", regex
Iii. How to use Nspredicate to filter a custom entity
Create an entity class person
@interface** * LastName; @end
We create entities of 5 person classes
Person *person1 = [personNew]; Person1.age= @ +; Person1.lastname=@"Zhang"; Person1.firstname=@"San"; person*person2 = [PersonNew]; Person2.age= @ -; Person2.lastname=@"Li"; Person2.firstname=@"si"; person*person3 = [PersonNew]; Person3.age= @ to; Person3.lastname=@"Wang"; Person3.firstname=@"Wu";
person*person4 = [PersonNew]; Person4.age= @ *; Person4.lastname=@"Zhao"; Person4.firstname=@"Liu"; person*PERSON5 = [PersonNew]; Person5.age= @ A; Person5.lastname=@"Test"; Person5.firstname=@"San";
And then create an array to save this object in.
Nsarray *personarray = [Nsarray arraywithobjects:person1,person2,person3,person4,person5, Nil];
Using Nspredicate to filter
// The predicate condition of the object that created the predicate is based on KVC, nspredicate* predicate = [nspredicate Predicatewithformat:@ "lastname= ' Wang ' and firstname== ' Wu ' " ]; for inch Personarray) { if([predicate Evaluatewithobject:person]) { NSLog (@ " person.lastname = %@", Person.lastname); } }
The print result is: Person.lastname = Wang
Nspredicate filtering An object logic is still very clear, but the trouble here is that there is also a loop, where we will find that the array provides
A method that makes it easier to retrieve an array object
As follows:
Nsarray *arraypre=[Personarray filteredarrayusingpredicate:predicate]; for inch arraypre) { NSLog (@ "person.lastname =%@", Person.lastname); }
See here that nspredicate can be used to retrieve and filter custom entities, if used for Nsstring,nsinteger,bool?
Let's look at the code again:
Nsarray *arrays = [Nsarray arraywithobjects:@"Jk_rui",@"Kerp",@"SOFT", nil]; Nspredicate*predicate = [Nspredicate predicatewithformat:@"self== ' SOFT '"]; Nsarray*resultarr =[Arrays filteredarrayusingpredicate:predicate]; for(NSString *tempstrinchResultarr) {NSLog (@"TempStr =%@", TEMPSTR); }
Print Result: TempStr = Spoft
iv. using Nspredicate to perform a differential sieve between two arrays
nsarray* array = @[@"Apple",@"IPhone"]; Nsarray* Array2 = @[@"Apple",@"IPhone",@"ipad",@"mac"]; Nspredicate* Thepredicate = [Nspredicate predicatewithformat:@"Not (self in %@)", array]; Nsarray* ARR3 =[Array2 filteredarrayusingpredicate:thepredicate]; NSLog (@"arr3=%@", ARR3);
The above code outputs the result arr3={@ "ipad", @ "Mac"}
v. How to use regular filter an array correctly
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");}Vi. Other notes and precautions regarding nspredicate, and tips
1. Dynamic Attribute Name
Let's say your code looks like this
Nspredicate *p = [nspredicate predicatewithformat:@ "name =%@"@ "name1 "
Obviously there is no problem with the code, but this is not the best way to do it I suggest the following:
Nspredicate *pretemplate = [nspredicate predicatewithformat:@ "name== $NAME"*dic =[nsdictionary dictionarywithobjectsandkeys:@ "name1"@"NAME "*pre=[pretemplate Predicatewithsubstitutionvariables:dic];
This may seem to make the code logic clearer.
2. When the filter condition field is dynamic
@" name " ; @" name1 " ; *p = [Nspredicate predicatewithformat:@ "%@ =%@", key, value];
Then the code will get an error when you execute to the third line!
There's no logical mistake!!! Why did it go wrong?
Nspredicate to automatically add quotes, so the final format should be @ "' name ' = ' name1 '". Obviously wrong. The thing to do is:
Nspredicate *p = [nspredicate predicatewithformat:@ "%K =%@"
Summary:
Nspredicate's simple use of the introduction to here, some of these are from the Internet or other blog to see, after the new needs to be perfected.
IOS verb nspredicate