谓词

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中的交集

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容