ios - SearchBarController 谓词导致崩溃

标签 ios objective-c uisearchbar nspredicate uisearchdisplaycontroller

我正在尝试将 searchBarController 添加到我的 tableView 中。我在构建应用程序时没有收到任何错误,但是当我开始在搜索栏中输入内容时,应用程序抛出异常并崩溃。

导致崩溃的行在最后一段代码中的下方进行了注释。有谁知道这里可能出了什么问题?非常感谢!

我的 .h 文件的相关部分:

@interface BusinessesViewController : UITableViewController<CLLocationManagerDelegate, 
UISearchDisplayDelegate> {

    IBOutlet UITableView *mainTableView;
    NSArray *businesses;
    NSMutableData *data;
    NSArray *filteredBusinesses;
}

我的 viewDidLoad 方法的相关部分:

- (void)viewDidLoad {
    filteredBusinesses = [[NSArray alloc]init];
}

NumberOfRowsInSection 方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [filteredBusinesses count];
    }
    else {
        return [businesses count];
    }
}

CellForRowAtIndexPath 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath {

    BusinessesTableViewCell *cell = (BusinessesTableViewCell *)[tableView 
    dequeueReusableCellWithIdentifier:@"MainCell"];

    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [cell.businessMainLabel setText:[[filteredBusinesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }   
    else {
        [cell.businessMainLabel setText:[[businesses 
        objectAtIndex:indexPath.row]objectForKey:@"name"]];
    }

    return cell;
}

Predicate 和 SearchDisplayController 方法:

- (void)filterContentForSearchText:(NSString *)searchText scope:(NSString *)scope {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"beginswith[cd] %@", 
    searchText];

    //THIS LINE OF CODE CAUSES THE CRASH
    filteredBusinesses = [businesses filteredArrayUsingPredicate:predicate];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller 
    shouldReloadTableForSearchString:(NSString *)searchString {

    [self filterContentForSearchText:searchString scope:
    [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:
    [self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    return YES;
}

最佳答案

您的谓词错误,丢失。从其他代码看起来好像 businesses 中的对象有一个“name”属性,因此谓词应该是

[NSPredicate predicateWithFormat:@"name BEGINSWITH[cd] %@",  searchText]
                                   ^key   ^operator    ^value

关于ios - SearchBarController 谓词导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25544320/

相关文章:

ios - 需要在 UILabel 的文本中获取多个@符号的 NSRanges 及其后面的文本

ios - 停止当前的 NSURLConnection

uisearchbar - 在位于 navigationItem titleView 的 ui 搜索栏右侧添加按钮

ios - 如何在最初通过 Safari 14/iOS 14 加载的 PWA 中保持登录状态?

ios - 使用 NavigationView 和 List 时如何更改 SwiftUI 中的背景颜色?

ios - 如何通过 fbconnect api 或 fbgraph api 向好友收件箱发送消息?

objective-c - 如何在 Swift 中使用 NSSet<Class>(导出为 Set<NSObject>)?

ios - AVPlayerItem 奇怪的缓冲区观察者

iphone - 寻找一种更好的方法来隐藏 View 顶部的搜索栏

ios - 当键盘在 iPad 上消失时,UISearchBar 取消按钮被禁用