iOS-谓词、模糊查询
2016-08-24 本文已影响212人
亦晴工作室
// 准备数据
NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:30];
for (NSInteger i = 0; i < 30; i++) {
NSString *name = [NSString stringWithFormat:@"1000phone%04d", arc4random_uniform(10000)];
NSInteger age = arc4random_uniform(20) + 15;
Person *p = [Person personWithName:name age:age];
[arrayM addObject:p];
}
NSMutableArray *array = [NSMutableArray array];
NSLog(@"初始结果 - %@", arrayM);
//新建好的谓词语句 beginwith between
NSString *pstringt = [NSString stringWithFormat: @" name BEGINSWITH '1000phone' && name contains '0' && age BETWEEN {%d,%d} ",28,30];
// NSString *pstringt = @" name BEGINSWITH '1000phone' && age >=26 ";
NSPredicate * predidate = [NSPredicate predicateWithFormat:pstringt];
//进行谓词匹配
NSArray *array2 =[arrayM filteredArrayUsingPredicate:predidate];
NSLog(@"-----%@",array2);