首页 > 代码库 > NSPredicate 谓词
NSPredicate 谓词
第一次接触“谓词”是在大学课堂的 《离散数学》 里。当时觉得挺好,挺有用。从当时的阅历来看,找不到谓词的使用场景。
在开发中有很多地方可以使用谓词。谓词最基本的功能就是:if 语句的判断
第二功能:数据筛选
说了这么多,可能还是有人不懂,何为谓词;谓词的另一叫法:断定。
“今天下雨” 这是一句话 对于这句话 有两个可能的结果 下雨 或者 不下。如果下雨,说明断定是对的,否则,断定是错的。
对于谓词,你可以简单的理解为:一个结果是BOOL的判定语句。
在实际的项目开发中,谓词的用途无外乎以上两种:作为if的条件 、 数据晒攒
看一下iOS中如何使用谓词
@interface Person : NSObject//定义一个Person类,头文件如下
@property(nonatomic,copy)NSString *name;
@property(nonatomic,assign)int age;
@property(nonatomic,copy)NSString *gender;
@property(nonatomic,assign)float score;
- (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 = [Person personWithName:@"张三" age:20 gender:@"男" score:89];
Person *p2 = [Person personWithName:@"李四" age:24 gender:@"男" score:65];
Person *p3 = [Person personWithName:@"王五" age:30 gender:@"男" score:76];
Person *p4 = [Person personWithName:@"小红" age:20 gender:@"女" score:92];
Person *p5 = [Person personWithName:@"小强" age:22 gender:@"女" score:62];
Person *p6 = [Person personWithName:@"小马哥" age:25 gender:@"男" 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.persons addObject:p1];
[self.persons addObject:p2];
[self.persons addObject:p3];
[self.persons addObject:p4];
[self.persons addObject:p5];
[self.persons addObject:p6];
[self.persons addObject:p7];
[self.persons addObject:p8];
}
- (void)predicateSimpleUse//谓词的测试
{
Person *p = [self.persons objectAtIndex:0];
//person的 name 是否是张三 name是person的属性 注意: =和==是一样的
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = ‘张三‘"];
BOOL match = [predicate evaluateWithObject:p];
NSLog(@"%@",match?@"YES":@"NO");
//person的score是否 >= 90
predicate = [NSPredicate predicateWithFormat:@"score >= 90"];
for(Person *person in self.persons)
{
if([predicate evaluateWithObject:person])
{
NSLog(@"person:%@ %g",person.name,person.score);
}
}
//筛选出 score >= 90的 所有person放入新的数组中 对于NSMutableArray,还有个filterUsingPredicate:方法,原数组中筛选符合条件的元素
NSArray *results = [self.persons filteredArrayUsingPredicate:predicate];
NSLog(@"%@",results);
//含有变量的谓词
predicate = [NSPredicate predicateWithFormat:@"age = $Test"];
NSDictionary *dic = [NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithInt:24],@"Test", nil];
NSPredicate *newPre = [predicate predicateWithSubstitutionVariables:dic];
NSLog(@"new %@",newPre);
for(Person *person in self.persons)
{
if([newPre evaluateWithObject:person])
{
NSLog(@"person:%@ %d",person.name,person.age);
}
}
NSLog(@"\n");
//谓词还支持C语言中的一些常用运算符
predicate = [NSPredicatepredicateWithFormat:@"age > 20 AND age < 26"];
for(Person *person in self.persons)
{
if([predicate evaluateWithObject:person])
{
NSLog(@"person:%@ %d",person.name,person.age);
}
}
predicate = [NSPredicate predicateWithFormat:@"name < ‘zhang‘"];
for(Person *person in self.persons)
{
if([predicate evaluateWithObject:person])
{
NSLog(@"person:%@",person.name);
}
}
predicate = [NSPredicatepredicateWithFormat:@"score between{60,80}"];
for(Person *person in self.persons)
{
if([predicate evaluateWithObject:person])
{
NSLog(@"person:%@ %g",person.name,person.score);
}
}
NSArray *betweens = [NSArrayarrayWithObjects:[NSNumbernumberWithFloat:60],[NSNumbernumberWithFloat:80], nil];
predicate = [NSPredicate predicateWithFormat:@"score between %@",betweens];
for(Person *person in self.persons)
{
if([predicate evaluateWithObject:person])
{
NSLog(@"person:%@ %g",person.name,person.score);
}
}
predicate = [NSPredicatepredicateWithFormat: @"name IN { ‘张三‘, ‘李四‘, ‘王五‘, ‘赵大‘ }"];
for(Person *person in self.persons)
{
if([predicate evaluateWithObject:person])
{
NSLog(@"person:%@ ",person.name);
}
}
//self可以出现在谓词里指的是使用谓词进行判定的对象
predicate = [NSPredicatepredicateWithFormat: @"self.name IN { ‘张三‘, ‘李四‘, ‘王五‘, ‘赵大‘ }"];
for(Person *person in self.persons)
{
if([predicate evaluateWithObject:person])
{
NSLog(@"person:%@ ",person.name);
}
}
//BEGINSWITH,ENDSWITH,CONTAINS
//附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分
//beginswith
NSMutableArray *array =[NSMutableArrayarrayWithObjects:@"Nick", @"Ben", @"Adam", @"Melissa", nil];
NSPredicate *bPredicate = [NSPredicatepredicateWithFormat:@"SELF beginswith[c] ‘a‘"];
NSArray *beginWithB = [array filteredArrayUsingPredicate:bPredicate];
NSLog(@"%@",beginWithB);
//contains
NSPredicate *sPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] ‘e‘"];
[array filterUsingPredicate:sPredicate];
NSLog(@"array = %@",array);
//like运算符 支持通配符? * ?表示1个字符 *表示若干字符
predicate = [NSPredicatepredicateWithFormat:@"name like [cd] ‘小*‘"];
for(Person *person in self.persons)
{
if([predicate evaluateWithObject:person])
{
NSLog(@"person:%@ ",person.name);
}
}
//谓词支持 正则表达式 支持 keyPath
//想要更多了解 谓词语法 Documents中搜索Predicate format string syntax
//想要更多了解 谓词的使用 Documents中搜索using predicate
}
以下是调用的代码
[selfinitPersonData];
[selfpredicateSimpleUse];