NSPredicate
第一次接触“谓词”是在大学课堂的 《离散数学》 里。当时觉得挺好,挺有用。从当时的阅历来看,找不到谓词的使用场景。 在开发中有很多地方可以使用谓词。谓词最基本的功能就是:if 语句的判断 第二功能:数据筛选 说了这么多,可能还是有人不懂,何为谓词;谓词的另一叫法:断定。 “今天下雨” 这是一句话 对于这句话 有两个可能的结果 下雨 或者 不下。如果下雨,说明断定是对的,否则,断定是错的。 对于谓词,你可以简单的理解为:一个结果是BOOL的判定语句。 在实际的项目开发中,谓词的用途无外乎以上两种:作为if的条件 、 数据晒攒 看一下iOS中如何使用谓词 @interfacePerson :NSObject//定义一个Person类,头文件如下 @property(nonatomic,copy)NSString*name;
- (id)initWithName:(NSString*)name age:(int)age gender:(NSString*)gender score:(float)score; + (id)personWithName:(NSString*)name age:(int)age gender:(NSString*)gender score:(float)score;
@end //在你的main或者 某类的实现文件里 添加两个方法 - (void)initPersonData//往数组里面添加若干Person对象 { Person*p1 = [PersonpersonWithName:@"张三"age:20gender:@"男"score:89]; Person*p2 = [PersonpersonWithName:@"李四"age:24gender:@"男"score:65]; Person*p3 = [PersonpersonWithName:@"王五"age:30gender:@"男"score:76]; Person*p4 = [PersonpersonWithName:@"小红"age:20gender:@"女"score:92]; Person*p5 = [PersonpersonWithName:@"小强"age:22gender:@"女"score:62]; Person*p6 = [PersonpersonWithName:@"小马哥"age:25gender:@"男"score:87]; Person*p7 = [PersonpersonWithName:@"steve jobs"age:56gender:@"男"score:60]; Person*p8 = [PersonpersonWithName:@"neal"age:26gender:@"男"score:90]; self.persons= [[[NSMutableArrayalloc]initWithCapacity:8]autorelease]; [self.personsaddObject:p1]; [self.personsaddObject:p2]; [self.personsaddObject:p3]; [self.personsaddObject:p4]; [self.personsaddObject:p5]; [self.personsaddObject:p6]; [self.personsaddObject:p7]; [self.personsaddObject:p8]; } - (void)predicateSimpleUse//谓词的测试 Person*p = [self.personsobjectAtIndex:0];
//person的name是否是张三name是person的属性注意: =和==是一样的 NSPredicate*predicate = [NSPredicatepredicateWithFormat:@"name = '张三'"]; BOOLmatch = [predicateevaluateWithObject:p]; NSLog(@"%@",match?@"YES":@"NO"); //person的score是否>= 90 predicate = [NSPredicatepredicateWithFormat:@"score >= 90"]; for(Person*personinself.persons) { if([predicateevaluateWithObject:person]) { NSLog(@"person:%@ %g",person.name,person.score); } } //筛选出score >= 90的所有person放入新的数组中对于NSMutableArray,还有个filterUsingPredicate:方法,原数组中筛选符合条件的元素 NSArray*results = [self.personsfilteredArrayUsingPredicate:predicate]; //含有变量的谓词 predicate = [NSPredicatepredicateWithFormat:@"age = $Test"]; NSDictionary*dic = [NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithInt:24],@"Test",nil]; NSPredicate*newPre = [predicatepredicateWithSubstitutionVariables:dic]; NSLog(@"new %@",newPre); if([newPreevaluateWithObject:person]) NSLog(@"person:%@ %d",person.age); NSLog(@"n"); //谓词还支持C语言中的一些常用运算符 predicate = [NSPredicatepredicateWithFormat:@"age > 20 AND age < 26"]; predicate = [NSPredicatepredicateWithFormat:@"name < 'zhang'"]; NSLog(@"person:%@",person.name); predicate = [NSPredicatepredicateWithFormat:@"score between{60,80}"]; NSArray*betweens = [NSArrayarrayWithObjects:[NSNumbernumberWithFloat:60],[NSNumbernumberWithFloat:80],sans-serif; font-size:14px"> predicate = [NSPredicatepredicateWithFormat:@"score between %@",betweens]; predicate = [NSPredicatepredicateWithFormat:@"name IN { '张三','李四','王五','赵大' }"]; NSLog(@"person:%@ ",sans-serif; font-size:14px"> //self可以出现在谓词里指的是使用谓词进行判定的对象 predicate = [NSPredicatepredicateWithFormat:@"self.name IN { '张三',sans-serif; font-size:14px"> //BEGINSWITH,ENDSWITH,CONTAINS //附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分 //beginswith NSMutableArray*array =[NSMutableArrayarrayWithObjects:@"Nick",@"Ben",@"Adam",@"Melissa",sans-serif; font-size:14px"> NSPredicate*bPredicate = [NSPredicatepredicateWithFormat:@"SELF beginswith[c] 'a'"]; NSArray*beginWithB = [arrayfilteredArrayUsingPredicate:bPredicate]; //contains NSPredicate*sPredicate = [NSPredicatepredicateWithFormat:@"SELF contains[c] 'e'"]; [arrayfilterUsingPredicate:sPredicate]; NSLog(@"array = %@",array); //like运算符支持通配符? * ?表示1个字符*表示若干字符 predicate = [NSPredicatepredicateWithFormat:@"name like [cd] '小*'"]; //谓词支持正则表达式支持keyPath //想要更多了解 谓词语法 Documents中搜索Predicate format string syntax //想要更多了解 谓词的使用 Documents中搜索using predicate 以下是调用的代码 [selfinitPersonData]; [selfpredicateSimpleUse]; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |