如果我的搜索谓词返回nil,则尝试弹出UIAlert

|| 如果用户进行搜索并且谓词返回nil,我将尝试弹出一条警告“未找到记录”的警报。 我尝试了此操作,但从未收到警报。我只是得到一个空的tableview。
- (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
。谓词是一个表达式,其结果为
true
false
。而已。 您可以向
NSFetchedResultsController
索取其
-fetchedObjects
(返回一个数组),然后向该数组索取其
-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];

    }   
}
    

要回复问题请先登录注册