Core data 如果搜索谓词返回nil,尝试弹出UIAlert
我试图弹出一个警告,如果用户进行搜索,谓词返回nil,则提示“未找到记录” 我试过了,但警报从未被调用。我只是得到一个空的桌面视图Core data 如果搜索谓词返回nil,尝试弹出UIAlert,core-data,uialertview,uisearchbar,nspredicate,Core Data,Uialertview,Uisearchbar,Nspredicate,我试图弹出一个警告,如果用户进行搜索,谓词返回nil,则提示“未找到记录” 我试过了,但警报从未被调用。我只是得到一个空的桌面视图 - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { if (self.sBar.text != nil){ NSPredicate *template = [NSPredicate predicateWithFormat:@"name contains[cd] $
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (self.sBar.text != nil){
NSPredicate *template = [NSPredicate predicateWithFormat:@"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH"];
NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@"SEARCH"];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
[[fetchedResultsController fetchedObjects] count];
if (fetchedResultsController.fetchedObjects != nil) {
//do nothing
} else {
//display alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"No records match."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort(); // Fail
}
[self.myTable reloadData];
[sBar resignFirstResponder];
}
谓词没有
-count
。谓词。就这样
您可以向NSFetchedResultsController
请求其-fetchedObject
(返回一个数组),然后向该数组请求其-count
,但您只能在调用-performFetch:
哈哈!明白了
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (self.sBar.text != nil) {
NSPredicate *template = [NSPredicate predicateWithFormat:@"name contains[cd] $SEARCH OR optionOne contains[cd] $SEARCH OR optionTwo contains[cd] $SEARCH"];
NSDictionary *replace = [NSDictionary dictionaryWithObject:self.sBar.text forKey:@"SEARCH"];
NSPredicate *predicate = [template predicateWithSubstitutionVariables:replace];
[fetchedResultsController.fetchRequest setPredicate:predicate];
}
NSError *error = nil;
if (![[self fetchedResultsController] performFetch:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
if ([[fetchedResultsController fetchedObjects] count] != 0) {
[self.myTable reloadData];
[sBar resignFirstResponder];
}
else {
//display alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"No records match."
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
…添加了FetchedObject计数,但仍然没有任何结果。@RyeMAC3您是否先执行了
performFetch:
?