NSPredicate predicate usage in IOS development, iosnspredicate Predicate
Programmers should have some knowledge about SQL statements, whether they have been in college or from training institutions, back-end or front-end. We know that, in SQL statements, where condition expressions can filter data in two-dimensional Relational Tables. microsoft C #. net also implements a technology that can rival SQL statements. It is a Lambda expression of a List generic set and supports searching, sorting, comparison, and combination. although java does not integrate the implementation of List object operations in languages, third-party open-source libraries also implement this function. A powerful NSPredicate class is provided in the Cocoa framework for IOS development. Here we will discuss where it is powerful...
NSPredicate inherits from NSObject and has two derived subclasses.
• NSComparisonPredicate
• NSCompoundPredicate)
Speaking of predicates, Let's first look at the syntax of the predicates.
1. Comparison Operators
*>: Greater
* <: Less
* >=: Greater than or equal
* <=: Less than or equal
* =, =: Equal
*! =, <>: Not equal
2. logical operators
* And/& and
* Or/| or
* Not /! Non
3. Relational operators
* ANY, SOME
* ALL elements
* NONE no element is equivalent to not any
* In inclusion
4. Range Operators
* Between, for example, 1 BETWEEN {0, 33}, or $ input between {$ LOWER, $ UPPER }.
* In inclusion
4. string itself
* SELF, for example, @ "self = 'appleios '"
5. String-related
* Contain
*
* Endswith
6. like wildcard
* Like: @ "name like [cd] '* ios *'"
@ "Name" like [cd] 'ios *'"
7. Regular Expression matches
* For example, NSString * regex = @ "^ A. + e $"; // starts with A and ends with e.
@ "Name MATCHES % @", regex
8. Array Operations
* Array [index]: Specifies the element at a specific index in the array.
* Array [first]: Specify the first element.
* Array [last]: Specifies the last element.
* Array [size]: Specifies the array size.
Let's take a look at the specific example below:
Create a project, and then add the products class
Products.h
1 // 2 // Products. h 3 // NSPredicateTest 4 // 5 // Created by xuhongjiang on 15/10/27. 6 // Copyright (c) 2015 xuhongjiang. all rights reserved. 7 // 8 9 # import <Foundation/Foundation. h> 10 11 @ interface Products: NSObject12 @ property NSString * productName; 13 @ property NSInteger productCount; 14 @ property NSString * productImageUrl; 15 + (id) initProductWithName :( NSString *) name withCount :( NSInteger) count withImage :( NSString *) imageurl; 16 @ end
Products.m
1 // 2 // Products. m 3 // NSPredicateTest 4 // 5 // Created by xuhongjiang on 15/10/27. 6 // Copyright (c) 2015 xuhongjiang. all rights reserved. 7 // 8 9 # import "Products. h "10 11 @ implementation Products12 + (id) initProductWithName :( NSString *) name withCount :( NSInteger) count withImage :( NSString *) imageurl13 {14 Products * sprducts = [[Products alloc] init]; 15 sprducts. productName = name; 16 sprducts. productCount = count; 17 sprducts. productImageUrl = imageurl; 18 return sprducts; 19} 20-(NSString *) description21 {22 NSString * str = [NSString stringWithFormat: @ "Product Name: % @, quantity: % ld, figure: % @ ", _ productName, _ productCount, _ productImageUrl]; 23 return str; 24} 25 @ end
Test method:
1 // 2 // ViewController. m 3 // NSPredicateTest 4 // 5 // Created by xuhongjiang on 15/10/27. 6 // Copyright (c) 2015 xuhongjiang. all rights reserved. 7 // 8 9 # import "ViewController. h "10 # import" Products. h "11 12 @ interface ViewController () 13 14 @ end 15 16 @ implementation ViewController 17 18-(void) viewDidLoad {19 [super viewDidLoad]; 20 [self mainTest]; 21} 22 23-(void) didReceiveMemo RyWarning {24 [super didReceiveMemoryWarning]; 25} 26 27-(void) mainTest 28 {29 Products * p1 = [Products initProductWithName: @ "A apple sdasf" withCount: 2 withImage: @ "464.jpg"]; 30 Products * p2 = [Products initProductWithName: @" fsdf orange gag "withCount: 53 withImage: @" fsdfas.jpg "]; 31 Products * p3 = [Products initProductWithName: @ "dfgdf banana" withCount: 5 withImage: @ "sfas.jpg"]; 32 Products * p4 = [Products initProdu CtWithName: @ "Samsung" withCount: 76 withImage: @ "ggas.jpg"]; 33 Products * p5 = [Products initProductWithName: @ "Huawei dfsd" withCount: 9 withImage: @ "gasa.jpg"]; 34 Products * p6 = [Products initProductWithName: @ "dhdhdhnnne" withCount: 6 withImage: @ "hshhh.jpg"]; 35 Products * p7 = [Products initProductWithName: @ "Samsung" withCount: 6 withImage: @ "hshhh.jpg"]; 36 Products * p8 = [Products initProductWithName: @ "15300250500" withCount: 6 withImage: @ "hshhh.jpg"]; 37 38 NSArray * sproducts = [NSArray arrayWithObjects: p1, p2, p3, p4, p5, p6, p7, nil]; 39 40 // number less than 9 define the predicate containing the filter condition 41 NSPredicate * prdicate = [NSPredicate predicateWithFormat: @ "productCount <% d", 9]; 42 // The filter result returns a new array 43 NSArray * newArray = [sproducts filteredArrayUsingPredicate: prdicate]; 44 for (Products * item in newArray) {45 NSLog (@ "newArray = % @", item. productName); 46} 47 48 49 // large quantity In 9 and productname is equal to the "Samsung jfggg" definition predicateWithFormat: @ "productName = 'samsung '& amp; productCount> 9"]; 51 // The filter result returns the new array 52 newArray = [sproducts filteredArrayUsingPredicate: prdicate]; 53 for (Products * item in newArray) {54 NSLog (@ "newArray =% @", item. productName); 55} 56 57 // in (inclusive) * Note that the inclusion is full-character match 58 prdicate = [NSPredicate predicateWithFormat: @ "productName IN {'G', 'huawei ', 'samsung '} | pro DuctCount IN {} "]; 59 // The filter result returns the new array 60 newArray = [sproducts filteredArrayUsingPredicate: prdicate]; 61 for (Products * item in newArray) {62 NSLog (@ "newArray = % @", item. productName); 63} 64 65 66 // productName 67 prdicate = [NSPredicate predicateWithFormat: @ "productName BEGINSWITH 'A'"] starting with a; 68 // productName end with ba 69 prdicate = [NSPredicate predicateWithFormat: @ "productName ENDSWITH 'G'"]; 70 71/ /Name 72 prdicate = [NSPredicate predicateWithFormat: @ "productName CONTAINS 'A'"]; 73 74 // like match any number of characters 75 // productName must have s characters and the conditions are met. 76 prdicate = [NSPredicate predicateWithFormat: @ "productName like's * '"]; 77 //? Represents a character. The following query condition is: The second character in name is s's 78 prdicate = [NSPredicate predicateWithFormat: @ "productName like '? S * '"]; 79 80 newArray = [sproducts filteredArrayUsingPredicate: prdicate]; 81 for (Products * item in newArray) {82 NSLog (@" newArray = % @ ", item. productName); 83} 84 85 // regular expression to verify whether the mobile phone number is 86 BOOL isMobileNum = [self isMobileNumber: p8.productName]; 87 if (isMobileNum) 88 NSLog (@ "is a real mobile phone number: % @ ", p8.productName); 89 90} 91 92 93-(BOOL) isMobileNumber :( NSString *) mobileNum 94 {95/** 96 * mobile phone number 97: 134 [0-8], 135,136,137,138,139,150,151,157,158,159,182,187,188 98 * China Unicom: 130,131,132,152,155,156,185,186 99 * China Telecom: 133,1349, 153,180,189 101 */NSString * MOBILE = @ "^ 1 (3 [0-9] | 5 [0-35-9] | 8 [025-9]) \ d {8} $ "; 102/** 103 10 * China Mobile: China Mobile104 11*134 [0-8], 135,136,137,138,139,150,151,157,158,159,182,187,188 105 12 */106 NSString * CM = @ "^ 1 (34 [0-8] | (3 [5-9] | 5 [017-9] | 8 [278 ]) \ d) \ d {7} $ "; 107/** 108 15 * China Unicom: china Unicom109 16*130,131,132,152,155,156,185,186 110 17 */111 NSString * CU = @ "^ 1 (3 [0-2] | 5 [256] | 8 [56]) \ d {8} $ "; 112/** 113 20 * China Telecom: China Telecom114 21*133,1349, 153,180,189 116 22 */349 NSString * CT = @ "^ 1 (33 | 53 | 8 [09]) [0-9] |) \ d {7} $ "; 117/** 118 25 * fixed line telephone and PHS 119 26 * area code: 010,020,021,022,023,024,025,027,028,029 120 27 * Number: seven or eight digits 121 28 */122 // NSString * PHS = @ "^ 0 (10 | 2 [0-5789] | \ d {3 }) \ d {123} $ "; 124 125 NSPredicate * regextestmobile = [NSPredicate predicateWithFormat: @" self matches % @ ", MOBILE]; NSPredicate * regextestcm = [NSPredicate response: @ "self matches % @", CM]; 126 NSPredicate * regextestcu = [NSPredicate primary: @ "self matches % @", CU]; 127 NSPredicate * regextestct = [NSPredicate primary: @ "self matches % @", CT]; 128 129 if ([regextestmobile evaluateWithObject: mobileNum] = YES) 130 | ([regextestcm evaluateWithObject: mobileNum] = YES) 131 | ([regextestct evaluateWithObject: mobileNum] = YES) 132 | ([regextestcu evaluateWithObject: mobileNum] = YES) 133 {134 if ([regextestcm limit: mobileNum] = YES) {135 NSLog (@ "China Mobile"); 136} else if ([regextestct evaluateWithObject: mobileNum] = YES) {137 NSLog (@ "Unicom"); 138} else if ([regextestcu evaluateWithObject: mobileNum] = YES) {139 NSLog (@ "Telecom "); 140} else {141 NSLog (@ "Unknow"); 142} 143 144 return YES; 145} 146 else147 {148 return NO; 149} 150} 151 @ end
1. query products with the number of products less than 9
The code here is very simple. The first step is to create a filter, replace number 9 with placeholders, the filter returns a new array, then traverse the array, and only take the product name.
// Number less than 9 define the predicate containing the filter condition NSPredicate * prdicate = [NSPredicate predicateWithFormat: @ "productCount <% d", 9]; // The new array NSArray * newArray = [sproducts filteredArrayUsingPredicate: prdicate]; for (Products * item in newArray) {NSLog (@ "newArray = % @", item. productName );}
2. query products with a quantity greater than 9 and productname equal to "Samsung jfggg"
// The number is greater than 9 and productname is equal to "Samsung jfggg". The defined predicateWithFormat includes the filter condition prdicate = [NSPredicate predicateWithFormat: @ "productName = 'samsung '& productCount> 9"]; // The new array newArray = [sproducts filteredArrayUsingPredicate: prdicate] is returned in the filter result.
3. in inclusion (* Note inclusion is full-character matching)
Prdicate = [NSPredicate predicateWithFormat: @ "productName IN {'G', 'huawei ', 'samsung'} | productCount IN {2, 5}"]; // The new array newArray = [sproducts filteredArrayUsingPredicate: prdicate] is returned in the filter result.
4. String-Related Processing
// Prdicate = [NSPredicate predicateWithFormat: @ "productName BEGINSWITH 'A'"]; // prdicate whose productName ends with ba = [NSPredicate predicateWithFormat: @ "productName ENDSWITH 'G'"]; // prdicate = [NSPredicate predicateWithFormat: @ "productName CONTAINS 'A'"];
5. like wildcard for fuzzy search
// Like matches any number of characters. // The condition prdicate = [NSPredicate predicateWithFormat: @ "productName like '* s *'"] is met as long as there are s characters in productName; //? The following query condition indicates that the second character in name is prdicate = [NSPredicate predicateWithFormat: @ "productName like '? S * '"]; newArray = [sproducts filteredArrayUsingPredicate: prdicate];
6. Regular Expression. The example is to verify whether it is a mobile phone number.
// Regular Expression string NSString * MOBILE = @ "^ 1 (3 [0-9] | 5 [0-35-9] | 8 [025-9]) \ d {8} $ "; // create a handsome selector NSPredicate * regextestmobile = [NSPredicate predicateWithFormat: @" self matches % @ ", MOBILE]; // The evaluateWithObject method of the filter reversely verifies whether the mobile phone number is used. The bool value BOOL isPhoneNum = [regextestmobile evaluateWithObject: @ "15300250500"] is returned.
We only list several common usage of predicates, and there are many flexible usage methods, for example, for Interval filtering of time datetime and predicate variable "Predicate = $ variable name", we hope you will study it later.