nspredicate
Nspredicate is a foundation class that specifies how data is fetched or filtered.
Its query language, like the intersection of SQL where and regular expressions, provides an expressive, natural language interface to define the logical conditions in which a collection is searched.
several usages of nspredicate
① Common Forms
Nspredicate *predicate = [nspredicate predicatewithformat:@ "name = = ' Zhangdan '"]; Note: If the block of text in the predicate string is not referenced, it is considered a key path, that is, you need to use quotation marks to indicate a string, single quotation mark, and double quotation mark. Key paths can contain many powerful features in the background
② calculation predicate
Let the predicate calculate its value by an object and give the bool value. For example:
Nspredicate *predicate = [nspredicate predicatewithformat: @ "name = = ' Zhangsan '"]; BOOL match = [predicate Evaluatewithobject:person]; NSLog (@ "%s", (match)? "YES": "NO"); Note that the person above is a human object
③ Fuel Filter
Filteredarrayusingpredicate: Is a class method of the Nsarray array that loops through the contents of the array and returns the object with the value Yes to the result array.
Nspredicate *predicate = [Nspredicate predicatewithformat:@ "n"]; Nsarray *results = [arr filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results);
④ Format Manual
%d and%@ represent inserting values and strings, and% K means that key can also introduce variable names, with $, like environment variables such as: @ "name = = $NAME", and then use Predicatewithsubstitutionvariables to construct a new predicate (Key/value dictionary , where the key is the variable name, the value is the content to insert, and note that in this case the variable cannot be used as a key path, only as a value.
predicate = [Nspredicate predicatewithformat: @ "name = =%@", @ "Zhangsan"]; predicate = [Nspredicate Predicatewithformat: @ "%K = =%@", @ "name", @ "Zhangsan"]; Nspredicate *predicatetemplate =[nspredicate predicatewithformat:@ "name = = $NAME"]; Nsdictionary *dic = [nsdictionary Dictionarywithobjectsandkeys: @ "Zhangsan", @ "NAME", nil]; Nspredicate *predicate = [predicatetemplate predicatewithsubstitutionvariables: dic]; NSLog (@ "%@", predicate);
⑤ comparison and logical operators
= = equals: Greater than >= and =>: greater than or equal to <: Less than <= and =<: less than or equal to! = and <>: Not equal to
Equivalent expressions for parentheses and logical operations and, or, not, or C-style && | | !
Note: The non-equal sign applies to numbers and strings
Nsarray *arrayfilter = [Nsarray arraywithobjects:@ "ABC1", @ "ABC2", nil]; Nsarray *arraycontent = [Nsarray arraywithobjects:@ "A1", @ "ABC1", @ "ABC4", @ "ABC2", nil]; Nspredicate *thepredicate = [nspredicate predicatewithformat: @ "not (self in%@)", Arrayfilter]; [Arraycontent Filterusingpredicate:thepredicate];
⑥ Array String
Between and in Add an array, you can use {50,200}, you can insert your own object with the%@ format specifier, or you can use a variable.
predicate = [Nspredicate Predicatewithformat: @ "Engine.horsepower between {$)"]; results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results); predicate = [Nspredicate predicatewithformat: @ "name in {' Herbie ', ' snugs ', ' Badger ', ' Flap '}"]; results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", [Results Valueforkey: @ "name"]);
⑦. String Operators
Beginswith ENDSWITH CONTAINS
[c]: suffix indicates case-insensitive [d]: no pronunciation symbols [CD]: suffix is case-insensitive and does not distinguish between pronounced symbols,
For example: @ "name CONTAIN[CD] ' ang '"//contains a string
@ "name Beginswith[c] ' sh '"//start with a string
@ "name Endswith[d] ' ang '"//End with a string
predicate = [Nspredicate predicatewithformat: @ "name Beginswith ' bad '"]; results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results); predicate = [Nspredicate predicatewithformat: @ "name ENDSWITH ' HERB '"]; results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results); predicate = [Nspredicate predicatewithformat: @ "name BEGINSWITH[CD] ' HERB '"]; results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results);
⑧.like operator
Like: The expression on the left equals the expression on the right:? and * can be used as wildcards, where? matches 1 characters, * matches 0 or more characters.
Like also accepts the [CD] symbol
predicate = [Nspredicate predicatewithformat: @ "name LIKE[CD] ' *er* '"]; results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results); predicate = [Nspredicate predicatewithformat: @ "name LIKE[CD] '??? Er* ' "]; results = [cars filteredarrayusingpredicate:predicate]; NSLog (@ "%@", results);
⑨.matches can use regular expressions
NSString *regex1 = @ "^a.+e$"; Starting with a, E ending nsstring *regex = @ "[a-za-z]+"; Nspredicate *predicate = [Nspredicate predicatewithformat:@ "Self MATCHES%@", regex]; if ([predicate evaluatewithobject:astring]) { }
⑩.self means that the object itself
Nsarray *arrayfilter = [Nsarray arraywithobjects:@ "ABC1", @ "ABC2", nil]; Nsarray *arraycontent = [Nsarray arraywithobjects:@ "A1", @ "ABC1", @ "ABC4", @ "ABC2", nil]; Nspredicate *thepredicate = [nspredicate predicatewithformat:@ ' not ' (self in%@) ", Arrayfilter]; [Arraycontent Filterusingpredicate:thepredicate];
①①. Total Operations
Any,some: Specifies any element in the following expression. For example, any children.age < 18.
All: Specifies all elements in the following expression. For example, all Children.age < 18.
None: Specifies an element that is not in the following expression. For example, NONE Children.age < 18. It is logically equal to not (any ...).
In: equals the in operation of SQL, the left expression must appear in the collection specified on the right. For example, name in {' Ben ', ' Melissa ', ' Nick '}.
①②. Nspredicate +predicatewithblock: (Block syntax)
Nspredicate *shortnamepredicate = [nspredicate predicatewithblock: ^bool (ID evaluatedobject, NSDictionary * Bindings) { return [[Evaluatedobject firstName] length] <= 5; }]; ["Alice Smith", "Bob Jones"] NSLog (@ "Short Names:%@", [people filteredarrayusingpredicate:shortnamepredicate]) ;
①③. Nscompoundpredicate
We have seen with & or are used in predicate format strings to create compound predicates. However, we can also use nscompoundpredicate to do the same job.
<span style= "FONT-SIZE:18PX;" > //For example, the following predicates are equal: [nscompoundpredicate andpredicatewithsubpredicates: @[[nspredicate predicatewithformat:@ "Age >"], [nspredicate predicatewithformat:@ "firstName =%@", @ "Quentin"]]; [Nspredicate predicatewithformat:@ (Age >) and (firstName =%@), @ "Quentin"];</span>
synthesis of predicates and regular expressions: (judging cell phone number, phone number function)
-(BOOL) Ismobilenumber: (NSString *) mobilenum{//Regular judgment mobile phone number address format/** * Mobile phone number * Mobile: 134[0-8],135,136,137,138,139,1 50,151,157,158,159,182,187,188 * Unicom: 130,131,132,152,155,156,185,186 * Telecom: 133,1349,153,180,189 */NSString * MOBILE = @ "^1 (3[0-9]|5[0-35-9]|8[025-9]) \\d{8}$"; /** * Mobile: China Mobile * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 * */nsstring * CM = @ "^1 (34[0-8]| ( 3[5-9]|5[017-9]|8[278] \\d) \\d{7}$ "; /** * Unicom: China Unicom * 130,131,132,152,155,156,185,186 * * * NSString * CU = @ "^1 (3[0-2]|5[256]|8[56]) \\d{8 }$"; /** * China Telecom: Chinese Telecom * 133,1349,153,180,189 * * * NSString * CT = @ "^1 ((33|53|8[09)) [0-9]|349] \\d{7}$"; /** * Mainland Area fixed and PHS * Area code: 010,020,021,022,023,024,025,027,028,029 * Number: seven-bit or eight-bit *///NSString * PHS = @ "^0 ( 10|2[0-5789]|\\D{3}) \\d{7,8}$ "; Nspredicate *regextestmobile = [Nspredicate predicatewithformat: @ "Self MATCHES%@", MOBILE]; Nspredicate *REGEXTESTCM = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CM]; Nspredicate *REGEXTESTCU = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CU]; Nspredicate *REGEXTESTCT = [Nspredicate predicatewithformat:@ "Self MATCHES%@", CT]; if ([regextestmobile evaluatewithobject:mobilenum] = = YES) | | ([regextestcm evaluatewithobject:mobilenum] = = YES) | | ([regextestct evaluatewithobject:mobilenum] = = YES) | | ([regextestcu evaluatewithobject:mobilenum] = = YES)) {if ([regextestcm evaluatewithobject:mobilenum] = = YES) {NSLog (@ "China Mobile"); } else if ([regextestct evaluatewithobject:mobilenum] = = YES) {NSLog (@ "China Telecom"); } else if ([regextestcu evaluatewithobject:mobilenum] = = YES) {NSLog (@ "China Unicom"); } else {NSLog (@ "Unknow"); } return YES; } else {return NO; }}
.
"Learning the path to iOS: UI series" Nspredicate related usage