首页 > 代码库 > 【学习ios之路:UI系列】NSPredicate相关用法
【学习ios之路:UI系列】NSPredicate相关用法
NSPredicate
NSPredicate是一个Foundation类,它指定数据被获取或者过滤的方式。
它的查询语言就像SQL的WHERE和正则表达式的交叉一样,提供了具有表现力的,自然语言界面来定义一个集合被搜寻的逻辑条件。
NSPredicate的几种用法
①常见形式
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == 'zhangdan'"]; //注意:如果谓词串中的文本块未被引用,则被看做是键路径,即需要用引号表明是字符串,单引号,双引号均可. 键路径可以在后台包含许多强大的功能②计算谓词
让谓词通过某个对象来计算自己的值,给出BOOL值.例如:
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name == 'zhangsan'"]; BOOL match = [predicate evaluateWithObject: person]; NSLog (@"%s", (match) ? "YES" : "NO"); //注上面的person是人对象③燃料过滤器
filteredArrayUsingPredicate:是NSArray数组的一种类别方法,循环过滤数组中的内容,将值为YES的对象累积到结果数组中返回.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"n"]; NSArray *results = [arr filteredArrayUsingPredicate: predicate]; NSLog (@"%@", results);④格式说明书
%d和%@表示插入数值和字符串,%K表示key 还可以引入变量名,用$,类似环境变量,如:@"name == $NAME",再用predicateWithSubstitutionVariables调用来构造新的谓词(键/值字典),其中键是变量名,值是要插入的内容,注意这种情况下不能把变量当成键路径,只能用作值.
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);⑤比较和逻辑运算符
==等于 >:大于 >=和=>:大于或等于 <:小于 <=和=<:小于或等于 !=和<>:不等于
括号和逻辑运算AND、OR、NOT或者C样式的等效表达式&& || !
注意:不等号适用于数字和字符串
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];⑥数组字符串
BETWEEN和IN后加某个数组,可以用{50,200},也可以用%@格式说明符插入自己的对象,也可以用变量.
predicate = [NSPredicate predicateWithFormat: @"engine.horsepower BETWEEN { 50, 200 }"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@", results); predicate = [NSPredicate predicateWithFormat: @"name IN { 'Herbie', 'Snugs', 'Badger', 'Flap' }"]; results = [cars filteredArrayUsingPredicate: predicate]; NSLog (@"%@", [results valueForKey: @"name"]);⑦.字符串运算符
BEGINSWITH ENDSWITH CONTAINS
[c]:后缀表示不区分大小写 [d]:不区分发音符号 [cd]:后缀不区分大小写和不区分发音符号,
例如: @"name CONTAIN[cd] ‘ang‘" //包含某个字符串
@"name BEGINSWITH[c] ‘sh‘" //以某个字符串开头
@"name ENDSWITH[d] ‘ang‘" //以某个字符串结束
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运算符
LIKE:左边的表达式等于右边的表达式:?和*可作为通配符,其中?匹配1个字符,*匹配0个或者多个字符。
LIKE也接受[cd]符号
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可以使用正则表达式
NSString *regex1 = @"^A.+e$"; //以A开头,e结尾 NSString *regex = @"[A-Za-z]+"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex]; if ([predicate evaluateWithObject:aString]) { }⑩.self就表示对象本身
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];①①.合计操作
ANY,SOME:指定下列表达式中的任意元素。比如,ANY children.age < 18。
ALL:指定下列表达式中的所有元素。比如,ALL children.age < 18。
NONE:指定下列表达式中没有的元素。比如,NONE children.age < 18。它在逻辑上等于NOT (ANY ...)。
IN:等于SQL的IN操作,左边的表达必须出现在右边指定的集合中。比如,name IN { ‘Ben‘, ‘Melissa‘, ‘Nick‘ }。
①②.NSPredicate +predicateWithBlock:(块语法)
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
我们见过与&或被用在谓词格式字符串中以创建复合谓词。然而,我们也可以用NSCompoundPredicate来完成同样的工作。
<span style="font-size:18px;"> //例如,下列谓词是相等的: [NSCompoundPredicate andPredicateWithSubpredicates: @[[NSPredicate predicateWithFormat:@"age > 25"], [NSPredicate predicateWithFormat:@"firstName = %@", @"Quentin"]]]; [NSPredicate predicateWithFormat:@"(age > 25) AND (firstName = %@)", @"Quentin"];</span>
谓词和正则表达式的综合:(判断手机号码,电话号码函数)
- (BOOL)isMobileNumber:(NSString *)mobileNum { // 正则判断手机号码地址格式 /** * 手机号码 * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 * 联通:130,131,132,152,155,156,185,186 * 电信:133,1349,153,180,189 */ NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$"; /** * 中国移动: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}$"; /** * 中国联通:China Unicom * 130,131,132,152,155,156,185,186 */ NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$"; /** * 中国电信:China Telecom * 133,1349,153,180,189 */ NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$"; /** * 大陆地区固话及小灵通 * 区号:010,020,021,022,023,024,025,027,028,029 * 号码:七位或八位 */ // 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; } }
.
【学习ios之路:UI系列】NSPredicate相关用法