objective-C IOS core data 使用NSPredicate查询
一个表里面有firstname ,lastname两个属性,我想查出firstname或者lastname中包含form的项
分别查我会,不知道怎么合并
NSString * searchTerm = [searchBar text];
[NSFetchedResultsController deleteCacheWithName:@"Master"];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
NSString *form = [NSString stringWithFormat:@"%@*",searchTerm ];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstname like %@)",form];
// NSLog(@"%@",searchBar.text);
[fetchRequest setPredicate:predicate];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstname" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
分别查我会,不知道怎么合并
NSString * searchTerm = [searchBar text];
[NSFetchedResultsController deleteCacheWithName:@"Master"];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
NSString *form = [NSString stringWithFormat:@"%@*",searchTerm ];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstname like %@)",form];
// NSLog(@"%@",searchBar.text);
[fetchRequest setPredicate:predicate];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstname" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
...全文
相关推荐
ios-NSPredicate用于查询.zip
Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取,用于数组与数组、数组与字符串之间之间数据查询,增删查改,数据比较
iOS中NSPredicate谓词的使用
前言 NSPredicate 谓词, 其实 NSPredicate 有时候完全可以被 [for…][if..else] 替换来实现功能, 但会造成代码不够优雅, 一堆的判断循环 有时一句 NSPredicate 便可实现. 今天作者就聊一聊谓词的使用. 一 NSPredicate 理解说明 NSPredicate : 中文直译谓词, 用来定义逻辑条件约束的搜索 或 内存中的过滤。 如同语法中的谓词, 如 [3 大于 2]中”大于”就是一个就是谓词. 简单点说 它是逻辑判断, 如同过滤器, 筛选你所需要的. NSPredicate 与 [for…][if..else] 功能有相同的
Swift_NSPredicate模糊、精确、查找Demo
Swift_NSPredicate模糊、精确、查找Demo
CoreDataUniqueConstraints:一个示例项目,用于获取可在核心数据中使用的唯一约束
CoreDataUniqueConstraints 这是一个用于在Core Data中设置唯一约束的示例项目。 您可以在我的博客文章中阅读有关内容
NSPredicate模糊搜索(全方面搜索)支持中文、数字、字母
可以实现可通过汉字,拼音,拼音首字母,拼音中间字母进行搜索的搜索栏。


