objective-C IOS core data 使用NSPredicate查询

zhanghanivy zhanghanivy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
一个表里面有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;
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
zhanghanivy
iOS 29,038 成员 12,459 内容