The IOS predicate is predicate logic. is similar to the SQL statement for a database, and is filtered from the data heap by criteria.
1. Usage Scenarios:
(1) Nspredicate left me the deepest impression is the two array to find the intersection of a requirement, if the general wording, requires 2 traversal, but Nsarray provides a filterusingpredicate method, with the Nspredicate, You don't have to traverse it.
(2) in an array of stored custom objects, you can query an array of objects that meet the criteria, based on criteria.
2. Familiarize yourself with predicate first:
Nsarray *array1 = [Nsarray arraywithobjects:@1,@2,@3,@5,@5,@6,@7, Nil];
Nsarray *array2 = [Nsarray arraywithobjects:@4,@5, Nil];
//means filter array1 elements in Array2! YES. Where self points to the caller of Filteredarrayusingpredicate.
/* Test Scenario:
Nspredicate *predicate1 = [Nspredicate predicatewithformat:@ "Self in%@", array2];
Nsarray *TEMP1 = [Array1 filteredarrayusingpredicate:predicate1];
Represents the Array1 element in the Array2
Results:
2015-11-08 10:55:19.879 nspredicatedemo[11595:166012] obj ==5
2015-11-08 10:55:19.879 nspredicatedemo[11595:166012] obj ==5
Nspredicate *predicate2 = [Nspredicate predicatewithformat:@ "Self in%@", array1];
Nsarray *TEMP1 = [Array2 filteredarrayusingpredicate:predicate1];
Results:
2015-11-08 10:55:19.879 nspredicatedemo[11595:166012] obj ==5
*/
Nspredicate *predicate1 = [Nspredicate predicatewithformat:@ "Self in%@", array2];
Nsarray *TEMP1 = [Array1 filteredarrayusingpredicate:predicate1];
[Temp1 enumerateobjectsusingblock:^ (id _nonnull obj, nsuinteger idx, BOOL * _nonnull stop) {
NSLog (@ "Temp1 =%@", obj);
}];
/*
2015-11-08 10:55:19.879 nspredicatedemo[11595:166012] obj ==5
2015-11-08 10:55:19.879 nspredicatedemo[11595:166012] obj ==5
*/
3. Basic syntax
(1) comparison operator >,<,==,>=,<=,!=
Available for numeric and string
(2) Scope operator: in, between
Example: @ "number between {1,5}"
@ "address in {' Shanghai ', ' Beijing '}"
(3) string itself: Self
Example: @ "self = = ' APPLE '"
(4) 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] case insensitive [d] does not distinguish between pronounced symbols and no accent marks [CD] are neither case sensitive nor pronounced.
(5) wildcard character: like
Example: @ "name LIKE[CD] ' *er* '"//* represents a wildcard, like also accepts [CD].
@ "name LIKE[CD] '??? Er* ' "
Specific code See Git:https://github.com/jiulin/nspredicatedemo
Text/90 Monkey vs Julin (Jane book author)
Original link: http://www.jianshu.com/p/b2694972863e
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".