什麼是謂詞?
謂詞是通過NSPredicate,是通過給定的邏輯條件作爲約束條件,完成對數據的篩選。
NSArray *array = [[NSArray alloc]initWithObjects:@"zhangsan",@"lisi",@"wangwu",@"xiaoliu",@"wulili",nil];
NSPredicate *preBegin= [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",@"w"];
//通過filteredArrayUsingPredicate過濾數組
NSArray *preBeginArray = [array filteredArrayUsingPredicate:preBegin];
NSPredicate *preContain = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",@"i"];
NSArray *preContainArray = [array filteredArrayUsingPredicate:preContain];
NSLog(@"preBeginArray>>%@",preBeginArray);
NSLog(@"preContainArray>>%@",preContainArray);
//初始化一個查詢條件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", @[@"bei", @"jing",@"huan",@"ying",@"ni"]];
if ([predicate evaluateWithObject:@"huan"]) {//判斷數據在不在結果集中
NSLog(@"huan");
};