NSPredicate多条件查询
来源:互联网 发布:linux 安装服务 编辑:程序博客网 时间:2024/06/09 16:54
谓词(NSPredicate):NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取,OC中的谓词操作是针对于数组类型(数组中可以存放对象)。
我就用之前写的有一篇关于搜索框筛选的,来讲这个多条件用法吧。链接地址:http://blog.csdn.net/qq_29892943/article/details/51831307
首先,在不知道NSPredicate的多条件查询用法时,我是用了多个数组来进行的查询
`-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
NSString *searchString = [self.searchController.searchBar text];//NSPredicate 多个条件查询NSPredicate *p1 = [NSPredicate predicateWithFormat:@"SELF.DiscussionName CONTAINS[c] %@", searchString];NSPredicate *p2 = [NSPredicate predicateWithFormat:@"SELF.userName CONTAINS[c] %@",searchString];if (self.searchList!= nil) { [self.searchList removeAllObjects];}//过滤数据NSMutableArray *onearray= [NSMutableArray arrayWithArray:[_SearchOnedataSource filteredArrayUsingPredicate:p1]];NSMutableArray *twoarray= [NSMutableArray arrayWithArray:[_SearchTwodataSource filteredArrayUsingPredicate:p2]];[self.searchList addObjectsFromArray:onearray];[self.searchList addObjectsFromArray:twoarray];//如果SELF.DiscussionName和SELF.userName是同一个人的名字,那么就会造成searchList里面出现两个重复的人,此时我们就需要移除数组中重复的数据了。而用nsset的allObjects则不会返回重复的元素。NSSet *set = [NSSet setWithArray:self.searchList];self.searchList=[NSMutableArray arrayWithArray:[set allObjects]];//刷新表格[self.tableView reloadData];
}`
上面这个方法能用,但是就是有点复杂,下面我们来看一下更直接的多条件查询
- (void)searchBar:(UISearchBar )searchBar textDidChange:(NSString )searchText;
{
NSString *searchString = [searchBar text];//NSString *searchString = searchText;if (self.searchList!= nil) { [self.searchList removeAllObjects];} //NSPredicate 多个条件查询 NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF.name CONTAINS[c] %@ || SELF.u_id CONTAINS[c] %@",searchString,searchString]; self.searchList= [NSMutableArray arrayWithArray:[_dataArray filteredArrayUsingPredicate:predicate]];
//刷新表格
[self.tableView reloadData];
}
在网上之前看到很多像什么这类的
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:
compoundPredicateArray ];还有[NSPredicate predicateWithFormat:@”(photosTookThere.@count != 0) AND (isFavourite == %@)”, [NSNumber numberWithBool:YES]];什么的,我试过都不行,可能这是用在别处的吧。反正我用起来不行。
下面写一些多条件的常用查询条件:
//年龄小于30 //定义谓词对象,谓词对象中包含了过滤条件 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30]; //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果 NSArray *array = [persons filteredArrayUsingPredicate:predicate]; NSLog(@"filterArray=%@",array); //查询name=1的并且age大于40 predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"]; array = [persons filteredArrayUsingPredicate:predicate]; NSLog(@"filterArray=%@",array); //in(包含) predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"]; //name以a开头的 predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"]; //name以ba结尾的 predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"]; //name中包含字符a的 predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"]; //like 匹配任意多个字符 //name中只要有s字符就满足条件 predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"]; //?代表一个字符,下面的查询条件是:name中第二个字符是s的 predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
关于这些NSPredicate的更多用法,大家可以参考这篇博客:http://blog.csdn.net/jiangwei0910410003/article/details/41923507
- NSPredicate多条件查询
- NSPredicate 条件查询 过虑
- NSPredicate 条件查询 过虑
- NSPredicate 条件查询 过虑
- NSPredicate 条件查询或过虑
- NSPredicate 条件查询或过虑
- NSPredicate 条件查询或过虑
- core data 查询条件 NSPredicate使用
- CoreData条件查询之NSPredicate应用
- ios 学习之 NSPredicate 模糊、精确、查询 ,包括模糊搜索,多条件搜索,及属性中多个字段检索
- ios NSPredicate NSPredicate 查询、搜索
- NSPredicate 条件过滤
- iOS 谓词查询NSPredicate
- IOS NSPredicate 查询、搜索
- IOS NSPredicate 查询、搜索
- IOS NSPredicate 查询、搜索
- iOS NSPredicate 查询、搜索
- iOS NSPredicate 查询、搜索
- 哈希表,二次探测再散列
- openVPN与域控的关联验证 以及问题解决过程
- Android Studio目录结构
- 使用匿名管道pipe例子
- 最基础的KMP模式串匹配
- NSPredicate多条件查询
- git打补丁(patch)
- JS变量重复声明以及忽略var 声明的问题及其背后的原理
- Codeforces Round Intel Code Challenge Final Round C. Ray Tracing
- Slidingmenu侧滑菜单
- Android图像格式类及图像转换方法
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B
- 控件背景为四周虚线
- lamp之源码包安装apache