标签:c语言谓词
NSArray *persons = @[jack, tom, rose, lucy, bob, john, lily];
//指定谓词条件
NSPredicate *pre1 = [NSPredicate predicateWithFormat:@"age < 40"];
for (Person *person in persons) {
//条件匹配
//验证对象是否符合谓词pre1
BOOL bool1 = [pre1 evaluateWithObject:person];
if (bool1) {
NSLog(@"%@", person);
}
}
//初始化谓词
NSPredicate *pre = [NSPredicate predicateWithFormat:@"age < %d", 40];
//数组中的方法,将符合谓词条件的数据筛选出来,返回一个新数组
NSArray *filteredArr = [persons filteredArrayUsingPredicate:pre];
NSLog(@"filteredArr is %@", filteredArr);
//逻辑运算符
// 运算符号 的加入 谓词不区分大小 && AND || OR
NSPredicate *pre3 = [NSPredicate predicateWithFormat:@"name = ‘rose‘ && age < %d", 30];
NSArray *array4 = [persons filteredArrayUsingPredicate:pre3];
NSLog(@"array4 is %@", array4);
// 关键字 注意字符串一定要添加‘‘
NSPredicate *pre4 = [NSPredicate predicateWithFormat:@"name IN {‘rose‘, ‘bruse‘, ‘jack‘}"];
NSArray *array5 = [persons filteredArrayUsingPredicate:pre4];
NSLog(@"%@", array5);
//BEGINSWITH 检查某个字是否以**开头
NSPredicate *pre5 = [NSPredicate predicateWithFormat:@"self.name BEGINSWITH ‘j‘"];
NSArray *array6 = [persons filteredArrayUsingPredicate:pre5];
NSLog(@"%@", array6);
//ENDSWITH 检查某个字符是以**结尾
NSPredicate *pre6 = [NSPredicate predicateWithFormat:@"self.name endswith ‘e‘"];
NSArray *array7 = [persons filteredArrayUsingPredicate:pre6];
NSLog(@"%@", array7);
// CONTAINS 检查包含某个字符
NSPredicate *pre8 = [NSPredicate predicateWithFormat:@"self.name CONTAINS ‘os‘"];
NSArray *array8 = [persons filteredArrayUsingPredicate:pre8];
NSLog(@"%@", array8);
标签:c语言谓词
原文地址:http://10554237.blog.51cto.com/10544237/1685763