谓词

NSPredicate  通过给定的逻辑条件作为约束条件,完成对数据的筛选。

NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];

//查询出包含e这个字符的字符串

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] '*e*' "]; //*表示通配符 NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];

predicate = [NSPredicate predicateWithFormat:@"customerID ==1"];

a = [customers filterArrayUsingPredicate:predicate];


NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age>20"];

//创建NSPredicate对象 并定义查询条件

NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];

//查询出以e这个字符结尾的字符串

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH[cd] 'e' "];

NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];

NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];

//查询出以a这个字符开头的字符串 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] 'a' "];

NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];

NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];

//查询出包含e这个字符的字符串

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] 'E' "];

NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];

NSArray *array1 = [NSArray arrayWithObjects:@1,@2,@3,@4,@5,@6,@7,@8, nil];

NSArray *array2 = [NSArray arrayWithObjects:@4,@6, nil];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in%@",array2];

//SELF 代表本身 IN可以大写也可以小写

NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; //表示获取 array2 和 array1中的交集

谓词_第1张图片
NSPredicate 的使用 - 勇敢的少年 - 博客园

本文来源 NSPredicate 的使用 - 勇敢的少年 - 博客园

你可能感兴趣的:(谓词)