我NSFetchedResultsController
用来存储类型的自定义对象:XMPPUserCoreDataStorageObject
. 我需要在我的 fetchedResultsController 中搜索字段displayName
ofXMPPUserCoreDataStorageObject
和搜索文本之间的匹配项。我通过以下方式访问该字段:
XMPPUserCoreDataStorageObject *user = [[self fetchedResultsController] fetchedObjects] objectAtIndex:i];
接着:
user.displayName;
其中i
是 中的对象数NSFetchedResultsController
。
如何使用格式制作谓词来比较user.displayName;
和searchText
?
我的搜索方法是:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if ([searchText length] == 0) {
fetchedResultsController = nil;
}
else {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"XXX contains[cd] %@", searchText];
[[fetchedResultsController fetchRequest] setPredicate:predicate];
[[fetchedResultsController fetchRequest] setFetchLimit:50];
}
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
[mainTableView reloadData];
}